Skip to content
Justice Tech School

abstract and sealed Classes

abstract classes cannot be created. sealed classes limit subclasses, perfect with when.

Example
sealed class Result
class Ok(val v: Int) : Result()
class Err(val msg: String) : Result()
fun show(r: Result) = when (r) {
    is Ok -> "ok ${r.v}"
    is Err -> "error ${r.msg}"
}
println(show(Ok(5)))
println(show(Err("bad")))

Exercise

Closed hierarchy.

 class Result

Quiz Test

Why sealed with when?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.