sealed Classes and Exhaustive switch
Close a hierarchy the compiler can check.
A Closed Family
A sealed class defines a fixed set of subtypes. No code outside its file can add a new one, so the family is fully known.
sealed class Shape {}Declaring Subtypes
Subclasses of a sealed type live in the same file. Together they form every possible variant the compiler must consider.
class Circle extends Shape {}
class Square extends Shape {}All lessons in this course
- Final Fields and Deep Immutability
- sealed Classes and Exhaustive switch
- base, interface, and final Modifiers
- copyWith and Value Equality