Ensuring proper encapsulation 00:00:00 Make a prediction You will find mutation paths that bypass an object's rules, including aliases supplied by the caller on the way in. An order has no setters, but returns its actual list of items. If a caller clears the list, what happens to a previously computed total? It can still report the old total for an empty order. Hiding fields is insufficient when a caller holds a mutable alias. Return a snapshot or restricted view and enforce mutations through the owner. 00:00:31 The sealed ant farm A sealed ant farm keeps the ants inside until somebody leaves the maintenance hatch open. Painting private on the glass does nothing about the open hatch. Returning a mutable collection or retaining a caller-owned input list is a second entrance into the state. Audit both directions, then choose a defensive copy or a genuinely safe view. Encapsulation keeps data together with the methods that use it, and hides the data. this design is about doing it properly: can code outside the class still break the object, although every field is private? If a rule can be broken from anywhere, then every line of the program is a suspect. If the rule can only be broken inside one class, the search is one file long. That is the practical value: bugs have an address. 00:01:24 Close the collection back door An order exposes its mutable item list. A caller removes the last item directly; the cached total still includes it. Keep the list private and return a read-only view or defensive copy suited to the language. Adding and removing items goes through the order. Remove one of two items through the public operation. Check both the resulting contents and the total, then verify a previously returned snapshot cannot change either. Start with two order lines and remove one through the order. The owner recomputes its total, so the displayed total still matches the remaining line. Then repeat by removing directly through a leaked list. A stale total exposes the missing ownership boundary without inspecting a private field. 00:02:12 Protect mutable inputs, too A schedule's constructor stores a caller-owned list of dates. The caller later clears that list, bypassing every method on the schedule. Copy the incoming collection where appropriate and consider whether its elements are mutable. Encapsulation includes aliases entering as well as leaving. Construct an order from a list and clear the original list afterward. Even a perfectly read-only getter cannot prevent that external alias from erasing the order. Copy the container on entry and decide separately whether any mutable elements need protection. A shallow copy of a list can still expose mutable elements. Try changing an item through a previously retained reference, not just appending to the container. When copying large data is too expensive, expose a controlled query or immutable elements instead of returning a writable internal structure. Java List.copyOf. The documentation says a later change of the given collection is not reflected in the result, and that an unmodifiable List is generally not copied again. Java Collections.unmodifiableList. Returns an unmodifiable view. Writing through it fails, and changes of the list behind it are visible. 00:03:32 Try an interview follow-up How would you close a leaking getter used by 60 callers? Find writers by search and by a logging list in production, add actions for each kind of write, move callers, then return an unmodifiable snapshot behind a flag so that remaining writers fail loudly. Afterwards a test and a lint rule keep it closed. An order rejects a negative quantity in add item. Its getter returns the mutable list of lines, so another caller can insert a negative line directly. Give callers a read-only view or copy as appropriate. Try mutating the returned collection and recheck the order total to test the boundary. 00:04:16 What to take with you An object is properly encapsulated when no call from outside can make its rules false. Private fields are needed, but references leak through constructors, getters, callbacks and subclasses. Every one of these is a door to check. Copy changeable input before checking it, return values or snapshots, and offer actions instead of setters. Use immutable values wherever you can: they need no copies. And do not seal what has no rule.