In this tutorial, we will explore an important feature of Kotlin programming language - Sealed Classes. Sealed classes are used to represent restricted class hierarchies, allowing a value to have one of the types from a limited set. They are often used for modeling data in a way that provides better type safety.
After completing this tutorial, you will understand:
- What sealed classes are
- How to create and use sealed classes
- How sealed classes can improve type safety and data modeling
Prerequisites:
- Basic knowledge of Kotlin programming language
- Familiarity with object-oriented programming concepts
In Kotlin, a sealed class is a class that can have a limited number of subclasses. Unlike regular classes, sealed classes allow you to restrict the class hierarchy. This means, you can control what types can be created from a sealed class.
To declare a sealed class, you use the sealed
keyword before the class
keyword:
sealed class MySealedClass
To use a sealed class, you define subclasses of it. You can define these subclasses either within the parent class or outside of it.
sealed class MySealedClass {
class SubClass1 : MySealedClass()
class SubClass2 : MySealedClass()
}
When you define a variable of the sealed class type, you can assign it instances of its subclasses:
val myVar: MySealedClass = SubClass1()
Sealed classes are useful in data modeling, as they provide better type safety. With sealed classes, you can create a restricted set of types, reducing the probability of runtime errors. They are often used in when
expressions, where they help ensure all possible cases are handled:
fun handleSealedClass(myVar: MySealedClass) {
when (myVar) {
is SubClass1 -> println("SubClass1")
is SubClass2 -> println("SubClass2")
}
}
sealed class Animal {
class Cat : Animal()
class Dog : Animal()
}
fun identifyAnimal(animal: Animal) {
when (animal) {
is Cat -> println("It's a Cat")
is Dog -> println("It's a Dog")
}
}
val myPet: Animal = Animal.Dog()
identifyAnimal(myPet) // Outputs "It's a Dog"
sealed class Result {
data class Success(val message: String) : Result()
data class Error(val error: String) : Result()
}
fun handleResult(result: Result) {
when (result) {
is Result.Success -> println(result.message)
is Result.Error -> println(result.error)
}
}
val res: Result = Result.Success("Operation successful.")
handleResult(res) // Outputs "Operation successful."
In this tutorial, you learned about sealed classes in Kotlin and how to use them for data modeling. You now know that they can provide better type safety and can be used to represent restricted class hierarchies.
To further your understanding, explore how to use sealed classes with data classes, how to define sealed class hierarchies, and how to use sealed classes with when
expressions.
Create a sealed class Shape
with two subclasses Circle
and Square
. Write a function describeShape
that takes a Shape
and prints a description of the shape.
Create a sealed class Response
with two data subclasses Success
and Failure
. Each subclass should hold a message. Write a function handleResponse
that takes a Response
and prints the message.
Expand on the Animal
sealed class from the first example. Add a Bird
subclass and update the identifyAnimal
function to handle this new subclass.
Remember to test your code and make sure it works as expected. Good luck with your Kotlin journey!