Sealed classes
A sealed class is a class with a limited number of subclasses (sealed subtyping hierarchy). Prior to Kotlin 1.1, these subclasses had to be defined inside a sealed class body. Kotlin 1.1 weakened this restriction and allowed us to define sealed class subclasses in the same file as a sealed class declaration. All the classes are declared close to each other, so we can easily see all possible subclasses by simply looking at one file:
//vehicle.kt
sealed class Vehicle()
class Car : Vehicle()
class Truck : Vehicle()
class Bus : Vehicle()To mark a class as sealed, simply add a sealed modifier to the class declaration header. The preceding declaration means that the Vehicle class can be only extended by three classes, Car, Truck, and Bus because they are declared inside the same file. We could add a fourth class in our vehicle.kt file, but it would not be possible to define such a class in another file.
The sealed subtyping restriction only applies to direct...