Encapsulation and Access Modifiers in Java (private, protected, public, default)
Fast recall
Quick revision
Core answer
withdraw() not setBalance()). Java has 4 access levels: private (this class only), default/package-private (no modifier — same package), protected (same package + subclasses elsewhere), public (everyone, permanently). Key trap: protected ≠ only subclasses — it includes the whole same package too.Whether the candidate understands encapsulation beyond 'make fields private' — can they explain what each access modifier controls, when to use package-private vs protected, and what real encapsulation looks like in practice
Thinking encapsulation means private fields plus getters/setters. Not knowing that default (no modifier) access is package-private, not public. Confusing protected with 'only subclasses' — it also includes everything in the same package
Explain that default access (package-private) is deliberate API design, not a forgotten modifier. Show that protected is wider than most think — same package plus subclasses anywhere. Demonstrate real encapsulation through behavior-exposing methods, not field-exposing accessors
Interview-ready guide
Interview answer
Encapsulation is about controlling the surface area of change — not just hiding fields. Every public method or field is a dependency: the wider you expose internals, the more places break when those internals change.
What encapsulation actually means
- Private fields plus getters/setters is indirection, not encapsulation —
account.setBalance(account.getBalance() - 100)puts all the logic on the caller, with no validation, no overdraft check, no audit log - Real encapsulation exposes behavior, not state:
account.withdraw(100)keeps all of that inside the class - Change
balancefromdoubletoBigDecimalwith real encapsulation — no callers notice, because they calledwithdraw(), nevergetBalance()
The four access modifiers
private— class only; no modifier (package-private) — same package;protected— same package plus subclasses in other packages;public— everyone, permanently- Interview trap:
protected≠ only subclasses — it includes the entire same package, making it strictly wider than package-private - There is no
defaultkeyword — you omit the modifier.default int countis a syntax error.
When to reach for each
- Start
private— widen only when there's a concrete reason - Package-private (no modifier): internal classes the same package needs but nothing outside should touch
- Every
publicmethod is a permanent API contract — once external code depends on it, removing it is a breaking change
Lombok's @Setter on every field undoes this entirely. If a field needs to change, there should be a method that describes why — not a setter that just sets.
Encapsulation and Access Modifiers in Java (private, protected, public, default)
Direct Answer
Encapsulation = the class owns its own state via behavior methods (withdraw() not setBalance()). Java has 4 access levels: private (this class only), default/package-private (no modifier — same package), protected (same package + subclasses elsewhere), public (everyone, permanently). Key trap: protected ≠ only subclasses — it includes the whole same package too.
Common Mistakes & Pitfalls
Thinking encapsulation means private fields plus getters/setters. Not knowing that default (no modifier) access is package-private, not public. Confusing protected with 'only subclasses' — it also includes everything in the same package
Check your understanding
Answer these without looking back at the article.
- What is the difference between protected and package-private — which one allows same-package access?
- Why doesn't making a field private and adding getters and setters constitute real encapsulation?
- When would you choose package-private (no modifier) over private for a class or method?
- How does Lombok's @Data or @Getter/@Setter annotation interact with encapsulation principles?
- What makes a method suitable for the public API vs keeping it as an internal implementation detail?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.