Sobes.tech
Junior — Middle

Is it permissible for a class declared as Sealed to inherit from another Sealed class?

sobes.tech AI

Answer from AI

In Kotlin, a class declared as sealed limits inheritance to within the same file where it is declared. Inheriting a sealed class from another sealed class is possible if it is done within the same file. However, sealed classes are typically used to create a restricted set of subclasses, and inheritance among them should be intentional and controlled.

Example:

sealed class Parent {
    class Child1 : Parent()
}

sealed class Child2 : Parent() {
    class GrandChild : Child2()
}

Here, Child2 inherits from Parent, both are sealed, and all are in the same file.