What Are Access Modifiers?
Access modifiers explained — public, private, protected and package-private in Java — and how they enforce encapsulation.
Expected Interview Answer
Access modifiers are keywords that control the visibility and accessibility of a class, method, or field — determining which other classes or packages are allowed to see or use them.
In Java, the four levels are “public” (accessible from anywhere), "protected" (accessible within the same package and by subclasses elsewhere), "default"/package-private (accessible only within the same package, used when no modifier is written), and “private” (accessible only within the declaring class itself). They are the primary enforcement mechanism behind encapsulation: making fields private and exposing only necessary behavior through public methods protects an object's internal state from uncontrolled external modification. Choosing the right modifier is a design decision — the general guideline is to make everything as restrictive as possible by default and widen access only when there is a genuine need, which minimizes coupling and keeps implementation details free to change.
- Enforces encapsulation by hiding internal implementation details
- Prevents unintended external modification of internal state
- Defines a clear, intentional public API surface for a class
- Reduces coupling between classes by limiting what is exposed
AI Mentor Explanation
A team's dressing room has different access rules: the changing area is private to the players, the balcony is protected for players and coaching staff, the practice nets are shared within the squad (package-private), and the match itself is public for anyone to watch. Access modifiers work identically in code — they set exactly who is allowed to see or touch a given field or method. A private strategy notebook stays with the player who owns it, never accessible from outside the team.
Step-by-Step Explanation
Step 1
Default to private
Declare fields private unless there is a specific reason for wider visibility.
Step 2
Expose behavior, not data
Use public methods to provide controlled, validated access to private state.
Step 3
Use protected for inheritance needs
Mark members protected only when subclasses genuinely need direct access.
Step 4
Use package-private for internal cohesion
Leave off the modifier to share helpers across classes in the same package without exposing them publicly.
What Interviewer Expects
- Correct listing and ordering of the four Java access levels
- Understanding of “narrowest access that still works” as the default guideline
- Connection between access modifiers and encapsulation
- Awareness that overriding methods cannot reduce visibility inherited from the parent
Common Mistakes
- Confusing protected with private (protected is still visible to subclasses and the package)
- Assuming default (no modifier) means public
- Making all fields public “to save time,” breaking encapsulation
- Not knowing overriding methods cannot narrow the parent method's access level
Best Answer (HR Friendly)
“Access modifiers are keywords like public, private, and protected that control who is allowed to see or use a class, method, or field. They're the main tool for enforcing encapsulation — by making internal data private and only exposing what's necessary through public methods, you protect an object's state and keep a clean boundary between a class's implementation and how others are allowed to use it.”
Code Example
public class Employee {
private double salary; // only this class
protected String department; // this class, subclasses, same package
String employeeId; // package-private (default)
public String name; // accessible everywhere
public double getSalary() { // controlled public access to private data
return salary;
}
public void setSalary(double salary) {
if (salary < 0) throw new IllegalArgumentException();
this.salary = salary;
}
}Follow-up Questions
- What is the difference between protected and package-private (default) access?
- Can a subclass reduce the visibility of an overridden method?
- Why is “private fields, public methods” the standard encapsulation pattern?
- How do access modifiers differ between Java and C++?
MCQ Practice
1. Which Java access modifier allows access from any class in any package?
public members are accessible from anywhere, with no restriction.
2. A member with no access modifier written (default) in Java is accessible from?
Package-private (default) access restricts visibility to classes within the same package.
3. Can an overriding method reduce the visibility of the method it overrides?
Overriding methods must keep the same or broader access level than the parent method, never narrower.
Flash Cards
Access modifiers in one line? — Keywords that control which classes can see or use a member.
Java's four levels? — public, protected, default (package-private), private.
Default visibility guideline? — Make members as restrictive as possible, widen only when genuinely needed.
What do access modifiers enforce? — Encapsulation — hiding internal state behind a controlled interface.