CHAPTER 17
Beginner
Data Classes and Sealed Classes
Updated: May 18, 2026
5 min read
# CHAPTER 17
Data Classes and Sealed Classes
1. Chapter Introduction
In Java, if you want a class just to hold data (like aUser with an ID and Name), you have to write the class, variables, getters, setters, a .toString() method, an .equals() method, and a .hashCode() method. It takes 50 lines of code just to hold a name!
Kotlin solves this instantly with Data Classes. In this chapter, we will learn how Data Classes eliminate boilerplate, and we will explore Sealed Classes, which act as advanced Enums for modeling specific application states.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Declare and use a
data class.
-
Understand the automatically generated methods (
toString,equals,copy).
- Destructure objects using component functions.
-
Define a
sealed class.
-
Use sealed classes with the
whenexpression for state modeling.
3. Data Classes
To create a class whose primary purpose is holding data, just add thedata keyword before class.
kotlin
By adding the data keyword, the compiler automatically generates:
-
1.
toString()(Prints nicely formatted data instead of memory addresses).
-
2.
equals()/==(Compares the actual data, not the memory reference).
-
3.
hashCode()
-
4.
copy()(To easily duplicate and alter immutable objects).
-
5.
componentN()functions (For destructuring).
kotlin
4. The copy() Method
Because data classes heavily encourage using immutable val properties, you cannot easily change a user's age. Instead, you create a *copy* of the user, altering only the property you want to change!
kotlin
5. Destructuring Declarations
Data classes generatecomponent functions that allow you to unpack an object into multiple variables instantly.
kotlin
6. Sealed Classes (Advanced State Modeling)
Enums are great for simple lists (likeMONDAY, TUESDAY). But what if you are building an Android app and making a network request? The state is either Loading, Success (which contains data), or Error (which contains a message). Enums cannot hold different types of data!
Sealed Classes are Enums on steroids. A sealed class restricts inheritance: all subclasses MUST be defined in the same file. This allows the compiler to know exactly how many states exist.
kotlin
7. Common Mistakes
-
Empty Data Classes: A data class MUST have at least one parameter in its primary constructor.
data class Empty()is an error.
-
Using
varin Data Classes: While allowed, it is terrible practice. Data classes should represent a snapshot of state. Always useval. If state changes, use.copy().
8. Best Practices
-
Exhaustive
when: Always usewhenas an expression (assigning it to a variable or usingreturn when) when dealing with Sealed Classes. This forces the compiler to error out if you add a new subclass to the Sealed Class but forget to handle it in thewhenblock!
9. Exercises
-
1.
Create a
data class Product(val id: Int, val name: String, val price: Double).
-
2.
Instantiate
Product(1, "Laptop", 999.99).
- 3. Print the object.
-
4.
Use
.copy()to create a new product with the same ID and Name, but a price of899.99.
10. MCQs with Answers
Question 1
What keyword creates a class that automatically generates toString() and equals()?
Question 2
In a data class, what does the == operator evaluate?
Question 3
What generated method allows you to easily duplicate an immutable data class while altering specific properties?
Question 4
What feature allows val (name, age) = user?
Question 6
What is a Sealed Class?
Question 7
Why are Sealed Classes superior to Enums for state modeling?
when expression with a Sealed Class, what massive benefit does the compiler provide?
a) It runs concurrently b) Exhaustive checking: The compiler ensures you have handled every possible subclass, preventing unhandled state bugs.
Answer: b) Exhaustive checking.
Question 9
Should data class parameters be val or var?
object Loading : NetworkResult() mean inside a sealed class?
a) It creates a Singleton (an object that only exists once in memory) to represent a state with no data. b) It creates an interface
Answer: a) It creates a Singleton object.
11. Interview Questions
-
Q: Explain what the Kotlin compiler generates behind the scenes when you use the
datakeyword.
- Q: Explain the difference between an Enum and a Sealed Class. Give an example of when you would use a Sealed Class in Android Development.