sealed class vs sealed interface: When to Use Each
Understand the difference between sealed classes and interfaces in Kotlin.
sealed class vs sealed interface: When to Use Each is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Sealed Types in Kotlin
sealed class Basics
sealed class Shape
class Circle(val radius: Double) : Shape()
class Rectangle(val w: Double, val h: Double) : Shape()
object Unknown : Shape()sealed interface: More Flexible
sealed interface Result<out T>
data class Success<T>(val value: T) : Result<T>
data class Error(val ex: Exception) : Result<Nothing>Key Differences
// sealed class subtype extends another class:
sealed interface Command
class MoveCommand(val dx: Int, val dy: Int) : Command, Serializable
class ResizeCommand(val scale: Double) : Command, CloneableExhaustive when: No else Needed
fun area(shape: Shape): Double = when (shape) {
is Circle -> Math.PI * shape.radius * shape.radius
is Rectangle -> shape.w * shape.h
} // no 'else' — compiler guarantees completenessAdding a Subtype Breaks Existing when
// Add Triangle to Shape:
class Triangle(val base: Double, val height: Double) : Shape()
// Now the area() function above won't compile until
// a Triangle branch is added — safety net!Sealed Classes with Common Properties
sealed class NetworkResult {
abstract val timestamp: Long
}
data class SuccessResult(val data: String, override val timestamp: Long) : NetworkResult()
data class FailureResult(val error: String, override val timestamp: Long) : NetworkResult()Nested Sealed Types
sealed interface UiState
object Loading : UiState
sealed interface Loaded : UiState
data class Content(val items: List<String>) : Loaded
data class Empty(val hint: String) : Loaded
data class Failure(val error: String) : UiStateobject Subtypes for No-State Variants
sealed class Direction
object North : Direction()
object South : Direction()
object East : Direction()
object West : Direction()
fun move(d: Direction) = when (d) {
North -> y++
South -> y--
East -> x++
West -> x--
}Sealed + data class + object Together
sealed class LoadState
object Idle : LoadState()
object Loading : LoadState()
data class Success(val count: Int) : LoadState()
data class Error(val msg: String) : LoadState()When to Choose sealed class vs interface
Quick Check
Recap
Frequently asked questions
Is the “sealed class vs sealed interface: When to Use Each” lesson free?
Yes — the full text of “sealed class vs sealed interface: When to Use Each” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “sealed class vs sealed interface: When to Use Each”?
Understand the difference between sealed classes and interfaces in Kotlin. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “sealed class vs sealed interface: When to Use Each” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- sealed class vs sealed interface: When to Use Each
- Exhaustive when with Sealed Hierarchies
- Modeling UI State with Sealed Classes
- Nesting and Combining Sealed Hierarchies