codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

16 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Sealed Classes
🎯Kotlin

Sealed Classes

Updated 2026-05-15
10 min read

Sealed Classes

Introduction

In Kotlin, sealed classes are a powerful feature that allow you to define restricted class hierarchies. Unlike regular classes, which can be inherited from anywhere in the codebase, sealed classes restrict their subclasses to be defined within the same file as the sealed class itself. This restriction is particularly useful for creating finite state machines or when you want to enforce certain constraints on your class hierarchy.

Concept

A sealed class is declared using the sealed keyword. The primary purpose of a sealed class is to ensure that all its subclasses are confined to the same file, making it easier to manage and reason about the class hierarchy. Sealed classes can have abstract methods, constructors, and properties just like regular classes.

Here are some key points about sealed classes:

  • Restriction on Subclasses: All subclasses of a sealed class must be defined within the same file as the sealed class.
  • Use Cases: They are often used in scenarios where you want to define a closed set of related classes, such as representing different states or types.
  • Pattern Matching: Sealed classes work well with Kotlin's when expression for exhaustive pattern matching.

Examples

Let's dive into some examples to understand how sealed classes work and how they can be used in practice.

Basic Example

Consider a scenario where you want to represent different states of an order in an e-commerce application. You can use a sealed class to define these states:

Kotlin
1sealed class OrderState {
2 object Pending : OrderState()
3 object Shipped : OrderState()
4 data class Delivered(val deliveryDate: String) : OrderState()
5 data class Cancelled(val reason: String) : OrderState()
6}
7
8fun handleOrder(state: OrderState) {
9 when (state) {
10 is OrderState.Pending -> println("Order is pending.")
11 is OrderState.Shipped -> println("Order has been shipped.")
12 is OrderState.Delivered -> println("Order delivered on ${state.deliveryDate}.")
13 is OrderState.Cancelled -> println("Order cancelled due to ${state.reason}.")
14 }
15}
16
17fun main() {
18 val order = OrderState.Delivered("2023-10-01")
19 handleOrder(order)
20}

In this example, OrderState is a sealed class with four subclasses: Pending, Shipped, Delivered, and Cancelled. Each subclass represents a different state of an order. The handleOrder function uses the when expression to perform exhaustive pattern matching on the OrderState.

Practical Use Case

Let's consider another practical example where sealed classes can be used to represent different types of shapes in a graphics application:

Kotlin
1sealed class Shape {
2 abstract fun area(): Double
3}
4
5data class Circle(val radius: Double) : Shape() {
6 override fun area(): Double = Math.PI * radius * radius
7}
8
9data class Rectangle(val width: Double, val height: Double) : Shape() {
10 override fun area(): Double = width * height
11}
12
13fun printArea(shape: Shape) {
14 println("The area of the shape is ${shape.area()}")
15}
16
17fun main() {
18 val circle = Circle(5.0)
19 val rectangle = Rectangle(3.0, 4.0)
20
21 printArea(circle)
22 printArea(rectangle)
23}

In this example, Shape is a sealed class with two subclasses: Circle and Rectangle. Each subclass implements the area() method to calculate its area. The printArea function takes a Shape object and prints its area.

What's Next?

In the next section, we will explore companion objects in Kotlin, which are static members of a class that can be used to define utility functions or properties related to the class.

By understanding sealed classes and their use cases, you can write more robust and maintainable code by restricting inheritance and leveraging exhaustive pattern matching.


PreviousData ClassesNext Companion Objects

Recommended Gear

Data ClassesCompanion Objects