Junior — Middle
How can a class be declared in Kotlin so that it can be extended in inheriting classes?
sobes.tech AI
Answer from AI
In Kotlin, all classes are final by default, meaning they cannot be inherited. To allow inheritance, a class must be declared with the open modifier.
Example:
open class Animal {
open fun sound() {
println("Some sound")
}
}
class Dog : Animal() {
override fun sound() {
println("Bark")
}
}
Here, Animal is declared as open, which allows the Dog class to inherit from it and override its methods.