Introduction
Inheritance lets a class acquire properties and functions from another class, forming an is-a relationship. Because Kotlin classes are final by default, both the base class and any member intended to be overridden must be explicitly marked open. A class can inherit from only one base class, but it can implement multiple interfaces.
Cricket analogy: Like a franchise team roster where a player must be explicitly registered as 'available for loan' before another franchise can pick them, a Kotlin class and its members must be explicitly marked open before another class can inherit and override them.
Syntax
open class Animal(val name: String) {
open fun makeSound() {
println("$name makes a sound")
}
}
class Dog(name: String) : Animal(name) {
override fun makeSound() {
println("$name barks")
}
}Explanation
The base class Animal is marked open, allowing it to be subclassed. Its makeSound() function is also marked open, permitting subclasses to override it. Dog inherits from Animal using the colon : syntax and immediately invokes the base class's primary constructor with Animal(name). The override keyword is mandatory in Kotlin when overriding a member, unlike Java's optional @Override annotation, which makes overriding intent explicit and catches signature mismatches at compile time. A class can extend exactly one base class, since Kotlin does not support multiple class inheritance, but it can implement several interfaces alongside that one base class.
Cricket analogy: Like a franchise's base training manual marked open, letting an academy team inherit and override the 'bowling technique' drill, Kotlin requires the explicit override keyword just as a scorecard must explicitly note when a batting order deviates from the template.
Example
open class Animal(val name: String) {
open fun makeSound() {
println("$name makes a sound")
}
}
class Dog(name: String) : Animal(name) {
override fun makeSound() {
println("$name barks")
}
}
fun main() {
val animals: List<Animal> = listOf(Animal("Generic"), Dog("Rex"))
for (a in animals) {
a.makeSound()
}
}Output
Generic makes a sound
Rex barksKey Takeaways
- A base class must be marked open to allow inheritance.
- Individual members must be marked open to allow overriding in subclasses.
- The override keyword is mandatory when overriding an open member.
- A class can inherit from only one base class (single inheritance).
- A class can implement multiple interfaces in addition to extending one base class.
- Subclasses must call the base class's primary constructor in their supertype list.
Practice what you learned
1. What must be added to a base class to allow it to be subclassed?
2. Is the override keyword optional in Kotlin when overriding an open function?
3. How many base classes can a Kotlin class directly extend?
4. What must a subclass do with the base class's primary constructor?
Was this page helpful?
You May Also Like
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Abstract Classes in Kotlin
Abstract classes provide a partial implementation with both abstract and concrete members that subclasses complete.
Interfaces in Kotlin
Explore how Kotlin interfaces define abstract methods, default implementations, and properties that classes can implement.
Visibility Modifiers in Kotlin
Visibility modifiers control where classes, functions, and properties can be accessed from in Kotlin code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics