?? Kotlin Deep Dive: Data Class & Sealed Class – Know the Differences & Use Cases
Muhammed Jasir
Software Developer at Wisebytes solutions | Jetpack Compose, Kotlin Coroutines, GitHub expertise
When working with Kotlin, Data Classes and Sealed Classes are powerful features that simplify code structure. Let’s break them down!
1. Data Class
A Data Class in Kotlin is designed to hold data with minimal boilerplate code.
Advantages:
- Automatically provides equals(), hashCode(), toString(), copy(), and componentN() methods.
- Ideal for representing plain data objects.
Disadvantages:
- Data classes can’t inherit from other classes.
- Requires at least one val or var property in the primary constructor.
Use Case:
- Perfect for POJOs (Plain Old Java Objects) or DTOs (Data Transfer Objects).
Example:
data class User(val name: String, val age: Int)
Comparison with Java:
- Kotlin: Data classes auto-generate commonly used methods, making the code more concise.
- Java: Requires manually overriding methods like equals(), hashCode(), and toString().
2. Sealed Class
A Sealed Class restricts the hierarchy to a limited set of types. You can think of it as an enum on steroids.
Advantages:
- Enforces type safety by ensuring all cases are known and handled.
- Useful in scenarios where you have a fixed number of possible classes.
Disadvantages:
- All subclasses of a sealed class must be defined in the same file.
Use Case:
- Ideal for representing restricted or hierarchical types, such as Result classes or handling UI state in Android apps.
Example:
sealed class Result {
data class Success(val data: String) : Result()
领英推荐
data class Failure(val error: Throwable) : Result()
}
Sealed Class vs Enum:
- Sealed Class: More flexible. Subclasses can hold different data types and use constructors.
- Enum: Limited to constants and can’t store complex data.
Example:
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
-----------------------------------------------------------------------------------------
??Most Asked Interview Questions & Short Answers:
1. What is the difference between Data Class and Regular Class in Kotlin?
Data classes auto-generate utility methods like equals(), hashCode(), toString(), and copy(). Regular classes do not.
2. What are the advantages of Sealed Classes?
Sealed classes enforce type safety by ensuring all possible subclasses are known and handled, making them great for modeling restricted hierarchies.
3. Can you compare Kotlin Sealed Classes and Java Enums?
Sealed classes allow complex data and hierarchy, while enums are limited to predefined constants without rich data types.
4. What is the use of the copy() function in Data Classes?
It creates a new instance of a data class with some properties modified while keeping the others unchanged.
5. Explain how Sealed Classes enforce exhaustive when expressions.
In when expressions, all subclasses of a sealed class must be handled, ensuring compile-time safety.
6. What are the limitations of Data Classes in Kotlin?
Data classes can’t inherit from other classes and must have at least one val or var property in the constructor.
7. Why would you choose Sealed Classes over Enums in Kotlin?
Sealed classes are more flexible, allowing subclasses with complex data, while enums are restricted to constants.
Got questions? Let’s discuss this in the comments!
P.S. If you found this helpful, don’t forget to share it with your network ??