Access modifiers in Java are essential for controlling the visibility and accessibility of classes, methods, and variables. They are fundamental to the principles of encapsulation, inheritance, and modular programming.
Java provides four primary access modifiers:
default
(package-private)public
private
protected
Let’s break down each one:
1. Default (Package-Private)
Keyword: No modifier
Visibility: Accessible only within the same package
Use Case: Internal logic that shouldn't be exposed outside the package.
Example:
java
class DefaultClass {
void greet() {
System.out.println("Hello from the same package!");
}
}
2. Public
Keyword: public
Visibility: Accessible from anywhere (any class or package)
Use Case: APIs, utility classes, or methods that need to be universally available.
Example:
java
public class PublicClass {
public void show() {
System.out.println("Visible everywhere!");
}
}
3. Private
Keyword: private
Visibility: Accessible only within the same class
Use Case: Internal state or helper methods that shouldn't be exposed outside the class.
Example:
java
public class PrivateExample {
private int secret = 42;
private void whisper() {
System.out.println("This is private info.");
}
}
4. Protected
Keyword: protected
Visibility: Accessible within the same package and in subclasses (even in different packages)
Use Case: Allowing subclasses to access or override certain behavior while still encapsulating it from the general public.
Example:
java
public class ProtectedExample {
protected void speak() {
System.out.println("Protected method");
}
}
5. Access Modifier Comparison Table
ModifierSame ClassSame PackageSubclass (Other Pkg)Other Packagesprivate
✅❌❌❌default
✅✅❌❌protected
✅✅✅❌public
✅✅✅✅
6. Example Demonstration
java
// File: com/example/MyClass.java
package com.example;
public class MyClass {
int defaultVar; // default (package-private)
public int publicVar;
private int privateVar;
protected int protectedVar;
void defaultMethod() {}
public void publicMethod() {}
private void privateMethod() {}
protected void protectedMethod() {}
}
java
// File: com/example/other/OtherClass.java
package com.example.other;
import com.example.MyClass;
public class OtherClass extends MyClass {
void testAccess() {
// defaultVar; ❌ Not accessible - different package
int b = publicVar; // ✅ Accessible
// int c = privateVar; // ❌ Inaccessible - private
int d = protectedVar; // ✅ Accessible - inherited
// defaultMethod(); ❌ Not accessible
publicMethod(); // ✅ Accessible
// privateMethod(); ❌ Inaccessible
protectedMethod(); // ✅ Accessible - inherited
}
}
7. Conclusion
Understanding access modifiers is crucial for writing secure, modular, and well-encapsulated Java code.
- Use
private
to protect internal data. - Use
public
to expose necessary APIs. - Use
protected
to share with subclasses. - Use the default modifier when you want package-level access only.
Choosing the right access modifier helps you control access, enforce encapsulation, and prevent unintended usage.