Java Access Modifiers — private, default, protected, public
Fast recall
Quick revision
Core answer
private (inside the declaring top-level class and its nested members), package-private or default (same package), protected (same package plus limited access from subclasses in other packages), and public (wherever the declaring type is accessible). Top-level classes can be only public or package-private.Understanding of encapsulation fundamentals and how Java enforces information hiding at compile time
Confusing protected with default — protected crosses package boundaries through inheritance, default never does
Explain the least-privilege principle: always start with private and relax only when necessary, explaining the design rationale
Interview-ready guide
Interview answer
Java has four access levels. private keeps a member inside its declaring class, package-private—written with no keyword—opens it to the package, protected adds inheritance-based access for subclasses in other packages, and public is the widest level.
The useful nuance is protected: a subclass in another package can use the inherited member through its own subclass context, not through any random instance of the superclass. I also distinguish members from top-level classes, because a top-level class can be only public or package-private. I normally begin with the narrowest access that callers actually need.
Java Access Modifiers — private, default, protected, public
Direct Answer
Java has four access levels: private (inside the declaring top-level class and its nested members), package-private or default (same package), protected (same package plus limited access from subclasses in other packages), and public (wherever the declaring type is accessible). Top-level classes can be only public or package-private.
Code Example
Seeing all four modifiers in one class makes the spectrum concrete. Each field below carries the narrowest access it can tolerate, and the methods expose only what the outside world legitimately needs.
package com.company.model;
public class BankAccount {
private double balance; // only BankAccount can access
String accountNumber; // default: same package only
protected String ownerName; // same package + subclasses anywhere
public String bankName; // accessible everywhere
public void deposit(double amount) { // public API
if (amount > 0) balance += amount; // only this class touches balance
}
protected double getBalance() { // subclasses can call this
return balance;
}
}
Notice the logic behind each choice: balance is the most sensitive value so it is private, while bankName is shared freely as public. The modifier on every line is really a one-word statement of who is allowed to depend on that member.
Common Mistakes & Pitfalls
- Reading "default" as more open than
protected— default actually blocks subclasses in other packages, whileprotectedlets them in; getting this backwards is exactly the trap interviewers set. - Treating package-private as the same as
public— a default member is invisible outside its package, so code in another module that used it as if it were public will not even compile. - Making fields
publicfor convenience — once external code readsaccount.balancedirectly, you can never add validation or change the representation without breaking every caller. - Assuming
privateis only "partly" restrictive — private is absolute; not even a subclass in the same file can see it, so reaching forprotectedwhen you meantprivatequietly widens your public surface.
Check your understanding
Answer these without looking back at the article.
- What is the default access modifier in Java?
- Can a subclass in a different package access a protected method?
- Why should class fields generally be private?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.