Interview Q&A / Java

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What are the four access levels in Java and who can see each one?

SHORT ANSWER

private is visible only inside the class. Default (no keyword) adds the same package. protected adds subclasses in other packages. public is visible everywhere.

Explain it simply

Think of a house. private is your own locked cupboard. Default is the living room, open to everyone in your family (the package). protected is the living room plus relatives who inherit the house (subclasses elsewhere). public is the front gate, open to anyone.

Example

java
package bank;

public class Account {
    private double balance;            // only Account
    int branchCode;                    // classes in package bank
    protected String holder;           // package bank + subclasses anywhere
    public String bankName;            // everyone
}
ModifierSame classSame packageSubclass elsewhereEverywhere
privateyesnonono
defaultyesyesnono
protectedyesyesyesno
publicyesyesyesyes

What to say in the interview

"I start with the tightest access that works: private for fields and helper methods, public only for the intended API. Default access is package-private, and the word default is not written. protected allows subclasses in other packages, but only through inheritance, not through a plain object reference of the parent type. Top-level classes can only be public or package-private."

Common mistakes

  • Thinking protected means subclass-only. It also gives access to the whole package.
  • Making fields public and giving up control over them.

#java#keywords#access-modifiers

Related questions

Practise more Java questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Java quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.