Sealed Classes
sealed classes restrict inheritance to within the same package. Code outside the package cannot add new subclasses. This allows the compiler to know all possible subtypes, enabling exhaustiveness checking (such as in match expressions) and optimizations.
Sealed classes are commonly used to represent finite state sets or type hierarchies. For example, a network request result has only two possibilities: success and failure. Using a sealed class ensures there won't be an unexpected third state. Sealed classes can be combined with abstract classes: sealed abstract class.
The Result below is a sealed abstract class that defines only two subclasses, Success and Failure, within the same package. The compiler guarantees no other subclasses will appear, making branch decisions and type-safe processing easier.
Subclasses of sealed classes can only be defined within the same package. Any inheritance attempt from outside the package will fail to compile.
Try adding a Pending subclass to represent a pending state, and make its message() return "Pending".