In Kotlin, classes and members are final by default, so you must explicitly mark them as open if you want them to be inherited or overridden.
Basic class inheritance
open class Animal {
open fun makeSound() {
println("Some sound")
}
}
class Dog : Animal() {
override fun makeSound() {
println("Bark")
}
}
Usage:
fun main() {
val dog = Dog()
dog.makeSound()
}
Output:
Bark
Key rules
1. Use open on a class to allow inheritance
open class Animal
Without open, this is not allowed:
class Dog : Animal()
because Animal would be final by default.
2. Use open on functions or properties to allow overriding
open fun makeSound() {
println("Some sound")
}
If a function is not marked open, subclasses cannot override it.
3. Use override in the subclass
override fun makeSound() {
println("Bark")
}
Kotlin requires override so it is clear that you are replacing behavior from the parent class.
Inheriting from a class with a constructor
If the parent class has a constructor, the subclass must call it:
open class Animal(val name: String) {
open fun introduce() {
println("I am $name")
}
}
class Dog(name: String) : Animal(name) {
override fun introduce() {
println("I am a dog named $name")
}
}
Usage:
fun main() {
val dog = Dog("Buddy")
dog.introduce()
}
Output:
I am a dog named Buddy
Overriding properties
Properties can also be open and override:
open class Animal {
open val sound: String = "Some sound"
}
class Dog : Animal() {
override val sound: String = "Bark"
}
Usage:
fun main() {
val dog = Dog()
println(dog.sound)
}
Output:
Bark
Calling the parent implementation with super
You can call the superclass version using super:
open class Animal {
open fun makeSound() {
println("Some sound")
}
}
class Dog : Animal() {
override fun makeSound() {
super.makeSound()
println("Bark")
}
}
Output:
Some sound
Bark
Preventing further overriding
An overridden member is open by default. If you want to prevent subclasses from overriding it again, mark it as final:
open class Animal {
open fun makeSound() {
println("Some sound")
}
}
open class Dog : Animal() {
final override fun makeSound() {
println("Bark")
}
}
Now subclasses of Dog cannot override makeSound().
Summary
open class Parent {
open fun method() {
println("Parent method")
}
}
class Child : Parent() {
override fun method() {
println("Child method")
}
}
open classmeans the class can be inherited.open funoropen valmeans the member can be overridden.overridemeans the subclass is replacing a parent member.- Use
super.method()to call the parent version.
