Encapsulation 00:00:00 Make a prediction You will put an account rule behind a public operation and prove that no caller can silently bypass it. An account holds twenty dollars. Its withdrawal method rejects thirty. Can the class still end up with a negative balance if its balance field is public? Yes. A caller can write minus ten without calling withdraw. The rule holds only if every balance-changing path passes through the owner that enforces it. 00:00:29 The popcorn cart At a popcorn cart one lever dispenses a portion; the pan and heating element stay behind the counter. Anyone can pull the lever, but cannot tip raw kernels directly into the customer's carton. The lever is the public operation; the cart owner controls each intermediate state. Affordable internals can change without teaching every customer a new sequence. Encapsulation has two halves. Bundling: data and the methods that work on it live in one unit, usually a class. Hiding: code outside the unit cannot reach the data directly. It asks through a small public list of methods, the interface. Rules stay true. A balance that only deposit and withdraw can touch obeys the rules of these two methods. A public balance obeys nobody. 00:01:20 Make overdrawing impossible An account holds twenty dollars. A caller requests thirty. If callers can write the balance, each caller must remember the rule. Give the account a withdrawal operation. It checks the amount and available funds before changing state. The operation refuses the request, and the balance stays twenty. A test checks both the result and the unchanged balance. Withdraw twelve from twenty. Eight remains, so a second withdrawal of twelve must leave that eight intact. Repeat the experiment with a UI that edits the numeric balance directly. Now every caller must rediscover the same refusal rule, and a single missed check creates an impossible balance. 00:02:04 Protect a booking from its callers A booking exposes its list of guests directly. Another object clears that list and leaves a confirmed booking with nobody attending. Return an immutable snapshot and provide an explicit remove guest operation. The booking can refuse removal of its last guest or cancel according to its own rules. Confirm a booking with two guests and hand its guest list to a reporting screen. Now let the screen sort or clear what it received. The confirmed booking must still own the same two guests. A read-only snapshot, plus checked operations for real guest changes, closes the escape hatch while keeping the report useful. Private fields plus a setter that accepts any balance recreate the same hole. Try a negative deposit as well as an excessive withdrawal. For a read-only display return a balance value or snapshot. It can be observed without handing the caller the ability to mutate it. Java changed the storage behind a String to represent common text more compactly. Callers still used the same public operations. SQLite offers another example: its database connection is an opaque handle. Clients call published operations to work with the connection, so its internal layout can change without requiring clients to know its fields. 00:03:23 Try an interview follow-up You must change a balance from a number into a ledger. What breaks? Nothing in code that uses the public methods. What can break: subclasses that use a protected field, reports that read the table, serialized objects, and callers that rely on speed. I would run the same public tests on both, migrate data with a rebuild of the total, and compare totals for a while. A shop changes today's catalogue price. A customer's old receipt now displays the new amount. Which fact escaped its proper owner? The old order relied on a live product price instead of owning the agreed sale amount and currency. Store those facts with the paid order. A subsequent price edit should change the next quote while leaving the old receipt identical. 00:04:17 What to take with you Encapsulation bundles data with the methods that work on it and keeps other code away from the data. Rules then live in one class, errors stop at the gate, and the inside can be replaced without touching callers. Use private fields, named actions and useful questions. Do not produce accessors by habit, and do not wrap plain values that have no rules. Test the boundary by trying to mutate a collection returned by the object. A getter can hand out a way to bypass a rule even if the field itself is private.