Loading…
Loading…
Charting your lesson…
Opening your lesson…
Encapsulation
Lesson 1 of 5
Bundling data with the methods that operate on it and hiding internal state behind a controlled interface.
6 steps•about 10 min•8 flashcards
Step 1/ 61 min
Encapsulation is the first of the four OOP pillars and the one interviewers probe with "why make fields private?" A crisp answer — bundling plus hiding, enforced through a controlled interface — signals you understand not just syntax but design intent.
Step 2/ 61 min
Encapsulation means bundling data (fields) together with the methods that operate on that data into a single unit (a class), and restricting direct access to the internal state. Outsiders interact only through a public interface (methods), while the internals stay private. This gives you data hiding and control over how state changes.
Encapsulation is realized with access modifiers:
The rule of thumb: make fields private, expose behavior through public methods. Reads/writes go through getters and setters (accessors/mutators), which can validate input.
Hiding internals lets a class enforce invariants. A BankAccount with a private balance can reject negative deposits in its setter; if balance were public, any code could corrupt it. Encapsulation also decouples callers from implementation: you can change how balance is stored (int, BigDecimal, a computed value) without touching any caller, because they only see the method signatures.
Encapsulation ("how" — hiding the internal state and wiring) is often confused with abstraction ("what" — exposing only essential behavior); they're related but distinct. Good encapsulation means a minimal public surface, immutability where possible, and not leaking mutable internals (e.g., returning a copy of a collection, not the live reference — otherwise callers can mutate your private state behind your back). Overusing trivial getters/setters that expose every field is a code smell that defeats the purpose — prefer methods that express behavior ("deposit"), not raw field access.
Step 3/ 61 min
A BankAccount keeps balance private. Deposits and withdrawals go through methods that validate: deposit rejects negative amounts; withdraw rejects amounts over the balance. Because no external code can set balance directly, the class guarantees the balance is always valid — the invariant "balance >= 0" is enforced in one place. Later you can add logging or currency conversion inside those methods without changing any caller.
Step 4/ 61 min
Always encapsulate state that has rules or invariants, or that you might want to change later. Expose behavior, not raw data. This is foundational to virtually every well-designed class.
Step 5/ 61 min
Remember "Encapsulation = capsule: data + methods sealed together, accessed only through the label." Keep fields private; expose behavior, not fields.
Don't equate "add a getter and setter for every field" with encapsulation. Blindly exposing every field through accessors gives callers full read/write access — barely better than public fields. True encapsulation exposes behavior and protects invariants.
Step 6/ 6· Worked examples1 min
Problem
Design a BankAccount so its balance can never go negative or be tampered with directly. Show what breaks without encapsulation.
Try it on paper first.
Recall
Keep this
Fast revision of encapsulation.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.