0% found this document useful (0 votes)
3 views

5. Access control in Java

Access control in Java restricts access to classes, methods, and variables through four access modifiers: private, default, protected, and public. Each modifier has specific accessibility rules, with private being the most restrictive and public the least. Understanding these modifiers is essential for ensuring security and proper encapsulation in Java programming.

Uploaded by

krishnapate2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

5. Access control in Java

Access control in Java restricts access to classes, methods, and variables through four access modifiers: private, default, protected, and public. Each modifier has specific accessibility rules, with private being the most restrictive and public the least. Understanding these modifiers is essential for ensuring security and proper encapsulation in Java programming.

Uploaded by

krishnapate2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Access control in Java

Access control in Java is a mechanism that restricts access to classes, methods, and variables to
ensure security. Java provides four levels of access control using access modifiers:

1. Access Modifiers in Java

Java has four access levels:

Modifier Class Package Subclass (Different Package) World (Everywhere)

private ✅ Yes ❌ No ❌ No ❌ No

default (no modifier) ✅ Yes ✅ Yes ❌ No ❌ No

protected ✅ Yes ✅ Yes ✅ Yes ❌ No

public ✅ Yes ✅ Yes ✅ Yes ✅ Yes

1. private

 Accessible only within the same class.


 Not accessible in subclasses or other classes in the same package.
 Example:

class Example {
private int data = 10; // Private variable

private void display () { // Private method


System.out.println("Data: " + data);
}
}

Note: The data and display() method are only accessible within the Example class.

2. default (No modifier)

 Accessible only within the same package.


 Example:

class Example {
int data = 10; // Default variable

void display() { // Default method


System.out.println("Data: " + data);
}
}

3. protected

 Accessible within the same package.


 Accessible in subclasses even if they are in different packages.

A package is a namespace that organizes related classes and interfaces. It helps prevent naming
conflicts by providing a unique namespace for classes, interfaces, and other components. This is
particularly useful in large projects(Emp class in ProjectA package and Emp class in package
ProjectB). We can reference both Emp classes using fully qualified names like
projectA.Employee empA = new projectA.Employee();

projectB.Employee empB = new projectB.Employee();

Types of Packages in Java


1. Built-in Packages – Predefined Java packages (e.g., java.util, java.io).
2. User-defined Packages –packages created by programmers like package1.

 Example:
package package1;
public class Parent {
protected int data = 20; // Protected variable

protected void display() { // Protected method


System.out.println("Data: " + data);
}
}

// Subclass in different package


package package2;
import package1.Parent;

class Child extends Parent {


void show() {
System.out.println(data); // Accessible due to inheritance
}
}

4. public

 Accessible from everywhere.


 Example:

package mypackage;

public class Example {


public int data = 30; // Public variable

public void display() { // Public method


System.out.println("Data: " + data);
}
}
Summary

 Use private for strict encapsulation.


 Use default for package-level access.
 Use protected for inheritance-based access.
 Use public for global access.

You might also like