Junior — Middle
Who can be declared as a successor to a class using the Sealed modifier?
sobes.tech AI
Answer from AI
A class with the sealed modifier in Kotlin restricts inheritance: only classes declared in the same file as the sealed class can inherit from it. This allows the compiler to know all possible subclasses and use this, for example, in when expressions without needing to write an else branch.
Example:
sealed class Result {
class Success(val data: String) : Result()
class Error(val error: Throwable) : Result()
}
// Inheriting from Result can only be done within this file