Preventing subclassing 00:00:00 Make a prediction You will decide which invariants need a closed type and how to preserve a separate extension point for legitimate variation. Money guarantees a valid currency and a fixed minor-unit amount. If a subclass can override the reported currency differently on each read, is constructor validation still enough? No. The guarantee depends on every exposed operation preserving it. Closing the money value can protect that contract while exchange policies remain independently extensible. 00:00:34 The brake on the hill A downhill cart has a brake calibrated for its wheels. Letting anyone replace the braking mechanism without meeting the stopping rule puts every rider at risk. A clip-on trailer can still be an intentional extension. Close the behaviour whose invariant callers rely on, and provide a separate extension point with a documented contract. Extensibility is a design choice, not a reward for every class. To prevent subclassing is to say that nobody may write a class that inherits from yours. Java and C++ say final class, C sharp says sealed class, Kotlin and Swift modules are closed unless you say open. Other code believes what your class promises. withdraw never lets the balance go below zero. A child that overrides withdraw can break that while still passing as an account. No reader of your code can see it. 00:01:29 Protect a validated amount A money value guarantees a valid currency and a whole number of minor units. An arbitrary subclass could override accessors and contradict those guarantees. Close the value type to extension if the language permits it. Let clients plug in a separate conversion policy. The policy returns a fresh validated money value. A new exchange provider is possible without allowing a subclass to redefine what the stored amount means. Give a transfer the same value twice. It relies on that value reporting a stable amount and currency both times. If a subclass can change the currency on its second read, even correct transfer code can break. Keep the value closed and move regional behaviour into a separate policy. 00:02:16 Choose a finite set of variants A payment result may be approved, declined or unknown. A sealed hierarchy can restrict implementations to these known cases and let a compiler check exhaustive handling. That is a different goal from forbidding every subtype of one concrete value. A client handles all three payment results. Introduce a fourth result, manual review, inside the controlled hierarchy. An exhaustive match can now flag every caller that must decide what manual review means; an open-ended family could silently steer it into the wrong fallback. A final class is still mutable if its methods or aliases mutate it. Closing inheritance solves a narrower problem than immutability. A sealed family allows a finite set of known alternatives when callers need exhaustive case handling rather than a single unextendable implementation. java.lang.String. Declared public final class String. Optional and the number wrapper classes are final too. Java records and enums. The language specification says that a record class is implicitly final, and an enum class is implicitly final or implicitly sealed. 00:03:31 Try an interview follow-up Should final be the default in your code base? For values and services behind interfaces yes, because opening is free and closing is not. For entities and proxied classes no, because the framework subclasses them. I write it as a short rule and let an architecture test check it. A framework generates subclasses for lazy-loaded records. Should the same final-class rule apply to those records and to a validated money value? The extension mechanisms serve different needs. Close invariants on the value object and document the framework-supported extension point on the entity. Test loading through the actual framework before imposing a repository-wide rule that would stop proxy creation. 00:04:22 What to take with you Close a class when other code relies on an invariant or on a value that does not change. Lock a method when children are welcome but one step must not be replaced. Seal a family when the kinds are known and a switch should be complete. Design for inheritance or prohibit it. An open class with no thought behind it is the dangerous middle.