protected vs default Access in Java
Fast recall
Quick revision
Core answer
protected also allows same-package access, and it gives a subclass in another package inheritance-based access. In that cross-package case, the subclass cannot access the protected instance member through an arbitrary superclass reference; it must use its own inherited view or a reference of the subclass type.Precise understanding of the protected vs default subtlety — a common interview trap question
Thinking default allows subclasses in other packages, or that protected is more restrictive than default
Give a concrete cross-package inheritance example showing protected access works but default fails
Interview-ready guide
Interview answer
Package-private means exactly what its name says: code in the same package can use the member, and code outside cannot. protected includes that package access and adds an inheritance path for a subclass in another package.
The detail I would mention is that cross-package protected access is tied to the subclass object. A Dog subclass can use the inherited member through this or another Dog, but not through an arbitrary Animal reference. That is the real difference, not simply saying 'protected works everywhere in subclasses.'
protected vs default Access in Java
Direct Answer
Package-private access (no keyword) stops at the package boundary. protected also allows same-package access, and it gives a subclass in another package inheritance-based access. In that cross-package case, the subclass cannot access the protected instance member through an arbitrary superclass reference; it must use its own inherited view or a reference of the subclass type.
Common Mistakes & Pitfalls
- Assuming
protectedmeans package-only — protected always includes same-package access and reaches subclasses in any package; treating it as package-scoped is the single most common mistake here. - Believing default lets subclasses in other packages access members — inheritance does not override the package boundary for default; the cross-package subclass simply won't compile.
- Ranking
protectedas stricter than default — for cross-package subclasses,protectedis actually the more permissive of the two, the opposite of what the name suggests. - Using
protectedwith no inheritance in mind — it invites outsiders to subclass your class just to reach its members; default keeps the surface tighter when extension was never a goal.
Check your understanding
Answer these without looking back at the article.
- Can a subclass in a different package access a default member of its parent?
- When would you choose protected over public?
- What is the package-private access level used for in framework design?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.