Deep Kotlin tutorial covering data class, enum, sealed class, sealed interface with examples, diagram, mistakes, and interview answers.

Data Classes, Enums, and Sealed Models

What is Data Classes, Enums, and Sealed Models?

Data Classes, Enums, and Sealed Models is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: data class, enum, sealed class, sealed interface, UI state. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Data Classes, Enums, and Sealed Models in simple language
  • Use data class in real Kotlin or Android code
  • Explain Data Classes, Enums, and Sealed Models in interviews with examples and trade-offs

How it works

Data Classes, Enums, and Sealed Models: data class, enum, sealed class, sealed interface, UI state works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

  1. Identify what problem Data Classes, Enums, and Sealed Models: data class, enum, sealed class, sealed interface, UI state solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • data class: core concept in this lesson
  • enum: core concept in this lesson
  • sealed class: core concept in this lesson
  • sealed interface: core concept in this lesson
  • UI state: core concept in this lesson
  • Class: blueprint that owns state and behavior
  • Interface: a contract with no state of its own
  • Inheritance: reusing behavior through an is-a relationship

Important differences

Concept 1Concept 2How to remember it
InheritanceCompositionInheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
InterfaceAbstract classAn interface is a pure contract with no state; an abstract class can hold shared state and partial implementation.
Data classSealed classA data class models one shape of data; a sealed class models a closed set of alternative shapes, useful for representing UI state.

Diagram

flowchart TD
A["1.5 Object-Oriented vs Functional Kotlin"] --> B["Data Classes, Enums, and Sealed Models"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Sealed class for UI state

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}

Delegation instead of inheritance

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source

When to use it

Use it when: Use this to model real domain concepts clearly - a data class for a User or Order, a sealed class for a finite set of UI states, an interface for a contract that several classes share.

Avoid it when: Avoid deep inheritance hierarchies for code reuse alone; prefer composition when the relationship is not a genuine is-a relationship.

Common mistakes

  • Reaching for inheritance when composition would be more flexible
  • Building deep inheritance hierarchies that are hard to change safely
  • Putting mutable shared state directly on an open base class
  • Using a plain class where a sealed class would make invalid states unrepresentable
  • Skipping interfaces and coding directly against a concrete implementation, which hurts testability

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Data Classes, Enums, and Sealed Models in 1.5 Object-Oriented vs Functional Kotlin?

Answer: Data Classes, Enums, and Sealed Models is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: data class, enum, sealed class, sealed interface, UI state. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

2. How does Data Classes, Enums, and Sealed Models work in Kotlin or Android code?

Answer: Data Classes, Enums, and Sealed Models: data class, enum, sealed class, sealed interface, UI state works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

3. What is one common mistake with Data Classes, Enums, and Sealed Models?

Answer: A common mistake is: Reaching for inheritance when composition would be more flexible Another is: Building deep inheritance hierarchies that are hard to change safely

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

4. How would you explain Data Classes, Enums, and Sealed Models with a small example?

Answer: Data Classes, Enums, and Sealed Models is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: data class, enum, sealed class, sealed interface, UI state. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

5. What is the trade-off of using Data Classes, Enums, and Sealed Models?

Answer: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

Practice task

Write one Kotlin example for Data Classes, Enums, and Sealed Models. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.