Sealed Classes

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.

How sealed classes work in Kotlin

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.

Get hands-on with 1400+ tech skills courses.