Kotlin Inheritance โ open, override, and super
Kotlin classes are final by default. Use open to allow inheritance. Subclasses override functions explicitly with override.
What You Will Learn
- Why classes are final by default
- Use open to enable inheritance
- Override properties and functions
- Call super in overrides
- Use abstract classes
open Classes
open and override
open class Animal(val name: String) {
open fun sound(): String = "..."
fun describe() = "$name says: ${sound()}"
}
class Dog(name: String) : Animal(name) {
override fun sound() = "Woof"
}
class Cat(name: String) : Animal(name) {
override fun sound() = "Meow"
}
fun main() {
val animals = listOf(Dog("Rex"), Cat("Whiskers"), Dog("Buddy"))
for (a in animals) println(a.describe())
}open class Animal can be subclassed. open fun sound() can be overridden. override fun sound() in the subclass provides a new implementation.
Abstract Classes
abstract class
abstract class Shape {
abstract fun area(): Double
fun describe() = "Area: ${"%.2f".format(area())}"
}
class Rectangle(val w: Double, val h: Double) : Shape() {
override fun area() = w * h
}
class Circle(val r: Double) : Shape() {
override fun area() = Math.PI * r * r
}
fun main() {
val shapes: List<Shape> = listOf(Rectangle(4.0, 5.0), Circle(3.0))
shapes.forEach { println(it.describe()) }
}abstract fun area() has no implementation โ subclasses must provide one. describe() is concrete and reuses area().
Calling super
super keyword
open class Vehicle(val brand: String) {
open fun info() = "Brand: $brand"
}
class Car(brand: String, val model: String) : Vehicle(brand) {
override fun info() = "${super.info()}, Model: $model"
}
fun main() {
val car = Car("Toyota", "Corolla")
println(car.info())
}super.info() calls Vehicle.info(). The Car then appends additional information.
Practice Exercise
What prints? open class Base { open fun greet() = "Hello from Base" } class Child : Base() { override fun greet() = super.greet() + " and Child" } fun main() { println(Child().greet()) }
Quick Quiz
Why are Kotlin classes final by default?
Frequently Asked Questions
Related Tutorials
Written by KotlinGuide Editorial Team ยท Reviewed by KotlinGuide Technical Review