Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › OOP III — Encapsulation, Abstraction, Enums

Encapsulation is more than private fields. What does proper encapsulation actually protect, and how do getters leak it?

Level II · JuniorOOP III — Encapsulation, Abstraction, Enums
Short answer

Encapsulation protects class invariants by ensuring all state changes go through controlled paths, so the object can never be observed in an invalid state.

Explanation

A getter that returns a reference to an internal mutable collection or array leaks that protection — the caller can mutate it directly and bypass validation. The fix is to return an unmodifiable view (Collections.unmodifiableList), a defensive copy, or an immutable type, depending on the cost and copy semantics you need.

Common mistakes
  • Saying private fields plus getters and setters for everything is encapsulation.
  • Returning internal mutable collections directly from getters.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for OOP III — Encapsulation, Abstraction, Enums.

Practise free

More on OOP III — Encapsulation, Abstraction, Enums