Junior — Middle
In Kotlin, are classes considered closed or open for inheritance by default?
sobes.tech AI
Answer from AI
In Kotlin, by default, all classes are final, meaning they cannot be inherited without explicit declaration.
To make a class inheritable, it must be declared with the open modifier:
open class BaseClass {
// class body
}
class DerivedClass : BaseClass() {
// subclass
}
This is done to enhance safety and prevent unintentional inheritance, which improves control over the class hierarchy.