Many classes exist only to hold data, and Kotlin makes them almost free to write. A data class automatically generates toString, equals, hashCode, copy, and component functions for destructuring, so you can model your data in a single line instead of pages of boilerplate.
You'll then meet sealed classes and interfaces, which restrict a class hierarchy to a known set of subtypes. Combined with when, they let the compiler check that every case is handled, which makes them ideal for modelling states and results. You'll also see how they compare to enum classes and when to choose each.
Watch the lessons in order, then continue on to Collections & Sequences.
Introduces data classes in Kotlin and the benefits you get from the functions they generate automatically.
Shows how to implement data classes, why we use them, and how the equals and hashCode functions work.
Introduces sealed classes and sealed interfaces in Kotlin and the differences between them and enum classes.
Compares sealed classes, sealed interfaces, and enum classes in Kotlin.