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
privatefor sensitive data. - β Misunderstanding
protectedaccess 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
protectedjudiciously 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
protectedand package-private?
A:protectedallows 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.