Data, Sealed & Value Classes
KOTLIN › Types & Classes
Kotlin's data, sealed, and value classes and choosing between enum and sealed.
Interviewers use these to probe how well you know Kotlin's modeling primitives and their compiler-generated behavior. Expect questions on what data classes generate and exclude, why sealed hierarchies make 'when' exhaustive, and how value classes give zero-overhead type safety. Knowing the exact constraints and runtime trade-offs separates idiomatic Kotlin from Java-in-Kotlin.
What this covers
- What a data class auto-generates (equals/hashCode/toString/copy/componentN) and which properties it uses
- Data class constraints: primary constructor with at least one val/var param; cannot be abstract, open, sealed, or inner
- copy() is a shallow copy and componentN() powers destructuring
- Sealed hierarchies are closed (same module and package) enabling exhaustive 'when' without else
- Value/inline classes wrap a single property with zero overhead, and when boxing occurs
- Choosing enum vs sealed for modeling a closed set of states
Study this topic
- Data, Sealed & Value Classes explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- Data, Sealed & Value Classes interview questions and answers