intermediate~3h
Java 17: Sealed Classes Finalize
Java 17, the second LTS release after 8 and 11, finalizes sealed classes -- a way to tell the compiler exactly which classes are allowed to extend a type, turning open-ended hierarchies into closed, exhaustively-checkable ones, and setting up exactly what pattern matching for switch needed to become safe.
Learning objectives
- Explain the exhaustiveness problem an open class hierarchy creates for switch and if/else chains
- Declare a sealed class or interface with a permits clause and know the rules for its permitted subtypes
- Compare an open hierarchy (interface + unlimited implementers) against a sealed one for the same domain model
- Explain how sealed classes combine with pattern matching for switch to produce compiler-checked exhaustiveness
- Identify when sealed classes are the right tool versus when an open interface or enum fits better
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Core Java Mastery library.
Java 17: Sealed Classes Finalize