Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. They are a cornerstone of encapsulation, one of the core principles of object-oriented programming (OOP).
📌 What are Access Modifiers?
- Definition: Keywords that define the scope and visibility of class members.
- Why it matters: Helps enforce encapsulation, security, and proper API design.
- When to use: Use them to control which parts of your code are exposed to other classes or packages.
[Related: link-to-other-article]
🔹 Types of Access Modifiers in Java
✅ 1. public
- Accessible from anywhere in the project.
- Used for APIs and classes meant to be globally available.
public class PublicClass {
public void show() {
System.out.println("Public method");
}
}
✅ 2. private
- Accessible only within the same class.
- Ideal for hiding implementation details and maintaining encapsulation.
class PrivateExample {
private int data = 100;
private void display() {
System.out.println("Private method");
}
}
✅ 3. protected
- Accessible within the same package and by subclasses (even in different packages).
- Useful for inheritance scenarios.
class Parent {
protected void greet() {
System.out.println("Protected method");
}
}
class Child extends Parent {
void callGreet() {
greet(); // Accessible due to inheritance
}
}
✅ 4. Default (Package-Private)
- When no modifier is specified, access is limited to the same package.
- Good for package-level encapsulation.
class DefaultExample {
void display() {
System.out.println("Default method");
}
}
📊 Comparison Table
Modifier | Same Class | Same Package | Subclass (diff package) | Other Packages |
---|---|---|---|---|
public | ✅ | ✅ | ✅ | ✅ |
protected | ✅ | ✅ | ✅ | ❌ |
default | ✅ | ✅ | ❌ | ❌ |
private | ✅ | ❌ | ❌ | ❌ |
🔹 Real-World Analogy
Think of access modifiers as locks on doors in a building:
- public: Open to everyone.
- protected: Only family and trusted guests have the key.
- default: Only people in the same apartment complex can enter.
- private: Only you have the key to your personal room.
🚫 Common Mistakes and Anti-Patterns
- ❌ Overusing
public
, exposing internal details. - ❌ Forgetting to use
private
for sensitive data. - ❌ Misunderstanding
protected
access across packages.
📈 Performance and Memory Implications
- Access modifiers have no direct impact on runtime performance.
- They influence design and maintainability rather than execution.
🔧 Best Practices
- Start with the most restrictive modifier (
private
) and relax only if needed. - Use
protected
judiciously to maintain proper inheritance control. - Avoid using default unintentionally; always be explicit when designing APIs.
📚 Interview Questions
-
Q: What is the default access modifier in Java?
A: Package-private, accessible only within the same package. -
Q: Can a top-level class be
private
?
A: No, only nested classes can beprivate
. -
Q: What’s the difference between
protected
and package-private?
A:protected
allows access in subclasses outside the package, while package-private doesn’t.
📌 Java Version Relevance
Java Version | Change |
---|---|
Java 1.0 | Introduced access modifiers |
Java 9 | Module system introduced, adding another layer of access control |
✅ Conclusion & Key Takeaways
- Access modifiers control visibility and enforce encapsulation.
public
,private
,protected
, and default serve different scopes.- Start with restrictive access and open up only when necessary.
❓ FAQ
Q: Do access modifiers apply to local variables?
A: No, they can only be applied to class members.
Q: Can constructors have access modifiers?
A: Yes, useful for patterns like Singleton.
Q: Are interface methods public by default?
A: Yes, all interface methods are implicitly public.