100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Inheritance in Kotlin

Learn how open classes, open members, and the override keyword enable single-class inheritance in Kotlin.

Classes & ObjectsIntermediate9 min readJul 8, 2026
Analogies

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

kotlin
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

kotlin
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

text
Generic makes a sound
Rex barks

Key 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

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#InheritanceInKotlin#Inheritance#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris