Preventing subclassing
medium 45 minA subclass can replace your methods, and then your promises are only as good as theirs.
Watch lesson4:44
Where it shows up
The same pattern appears across everyday software.
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.
Kotlin
By default, classes are final, says the documentation. `open` makes one inheritable, `sealed` limits direct children to the same module and package.
Swift open and public
SE-0117, implemented in Swift 3, made subclassing outside the module an explicit choice.
.NET analyzer CA1852
Suggests marking a type as sealed if it is not visible outside its assembly and has no subtypes there.
Python bool
The documentation says that it cannot be subclassed further. Its only instances are True and False.
Guava immutable collections
The documentation promises integrity: the types cannot be subclassed outside their package, so nobody can make a mutable child. You get instances from `of`, `copyOf` and builders.
Spring AOP
The reference lists it among the limits of CGLIB proxies: final classes cannot be proxied and final methods cannot be advised.
Hibernate
The user guide requires that an entity class and its persistent members are not final. Not everything should be closed.