Kotlin classes are final by default—unlike Java where classes are open unless marked final. Use open on the base class and override on members to customize behavior.
open, override, and super
open class Animal(val name: String) {
open fun speak() = "$name is quiet"
}
class Dog(name: String) : Animal(name) {
override fun speak() = "$name says woof"
}
Primary constructor parameters from the base class are passed in the derived header: class Dog(name: String) : Animal(name).
Important interview questions and answers
- Q: Why final by default?
A: Encourages composition over fragile inheritance—explicitopendocuments extension points. - Q: override keyword required?
A: Yes for overriding; base members intended for override must be markedopen.
Self-check
- What keyword makes a class inheritable?
- How do you call the superclass implementation?
Tip: Favor composition and interfaces over deep inheritance—same guidance as Java design.
Interview prep
- open keyword?
Required on class and members to allow override.
- Primary constructor in subclass?
Pass base args: class Dog(name) : Animal(name).