Using Sealed Classes for Data Modeling

Tutorial 2 of 5

Introduction

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

Step-by-Step Guide

What are Sealed Classes?

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

How to Use Sealed Classes?

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()

Type Safety and Data Modeling

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")
    }
}

Code Examples

Example 1: Basic Sealed Class

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"

Example 2: Sealed Class with Data

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."

Summary

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.

Practice Exercises

Exercise 1

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.

Exercise 2

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.

Exercise 3

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!