Understanding Java Access Modifiers
Access Modifier Table
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| private | ✅ | ❌ | ❌ | ❌ |
| default | ✅ | ✅ | ❌ | ❌ |
| protected | ✅ | ✅ | ✅ | ❌ |
| public | ✅ | ✅ | ✅ | ✅ |
1. private Access Modifier
- Visibility: The
privatemodifier restricts access to the members of the class only. Only the class where it is declared can access it. - Use Case: It’s typically used to implement encapsulation by hiding the details of an object.
Example:
class MyClass {
private int value; // Only accessible within MyClass
private void displayValue() {
System.out.println(value);
}
public void setValue(int value) {
this.value = value; // Allowed, because it's within the same class
}
}
2. default (Package-Private) Access Modifier
- Visibility: When no access modifier is specified, it has default access (package-private). Members with this modifier are only accessible within classes in the same package.
- Use Case: It is used when you want to restrict access to a package but allow any class within that package to access it.
3. protected Access Modifier
- Visibility: The protected modifier allows access to members from: The same package and Any subclass (even if it’s in a different package).
- Use Case:
protectedis used when a member should be available to subclasses and package members, but not to other classes outside the package and inheritance hierarchy.
4. public Access Modifier
- Visibility: The
publicmodifier allows members to be accessible from any other class in any package. - Use Case:
publicis used when you want to make a class, method, or variable accessible globally. This is the most permissive modifier.
5. Best Practices
- Encapsulation: Use
privatefor fields and methods to restrict access and provide public getter/setter methods if needed. - Package-level visibility: Use the default access modifier for classes and methods that should only be used within the same package.
- Inheritance: Use
protectedfor members that should be accessed by subclasses. - Global access: Use
publiconly when you need to expose functionality to the whole project or the external world.