September 2019
Intermediate to advanced
462 pages
11h 3m
English
In Kotlin, on one extreme we have final classes—that is, classes not marked as open—which can’t have any derived classes. On the other extreme we have open and abstract classes, and there’s no telling which class may inherit from them. It’ll be nice to have a middle ground for a class to serve as a base to only a few classes, which the author of the class specifies.
Kotlin’s sealed classes are open for extension by other classes defined in the same file but closed—that is, final or not open—for any other classes.
Here’s a sealed class Card, along with a few classes that inherit from it, all within the same file Card.kt.
| | sealed class Card(val suit: String) |
| | |
| | class Ace(suit: String) : Card(suit) |
| | |
| |
Read now
Unlock full access