Skip to main content
Kotlin Basics
CHAPTER 15 Beginner

Inheritance in Kotlin

Updated: May 18, 2026
5 min read

# CHAPTER 15

Inheritance in Kotlin

1. Chapter Introduction

Imagine you are building a game with Warrior and Mage characters. Both have health and a move() function, but they attack differently. Writing the health property and move() logic in every single character class violates the DRY (Don't Repeat Yourself) principle. Inheritance solves this. We can create a parent Character class containing the shared code, and have Warrior and Mage inherit from it! In this chapter, we explore how Kotlin handles inheritance, specifically highlighting its strict default behavior using the open keyword.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the concept of Parent (Super) and Child (Sub) classes.
  • Use the open keyword to allow inheritance.
  • Inherit properties and methods using the : syntax.
  • Override parent methods using the override keyword.
  • Call parent logic using the super keyword.

3. The open Keyword (Kotlin's Safety Feature)

In Java, any class can be inherited by default. This led to "Fragile Base Class" bugs, where developers would accidentally break child classes by modifying a parent. In Kotlin, all classes are final (locked) by default. You cannot inherit from a Kotlin class unless you explicitly mark it with the open keyword.
kotlin
123456
// The 'open' keyword allows other classes to inherit from this one
open class Animal(val name: String) {
    fun eat() {
        println("$name is eating.")
    }
}

4. Creating a Subclass

To inherit from a parent class, we use a colon : followed by the parent class name and its constructor.
kotlin
1234567891011121314151617
// Dog inherits from Animal
// We pass the 'name' from Dog's constructor directly into Animal's constructor!
class Dog(name: String) : Animal(name) {
    fun bark() {
        println("Woof!")
    }
}

fun main() {
    val myDog = Dog("Buddy")
    
    // myDog has access to its own methods...
    myDog.bark() 
    
    // AND it inherited the methods from Animal!
    myDog.eat()  
}

5. Method Overriding

Sometimes a child class wants to change how a parent method works. For example, a generic Animal might make a sound, but a Dog barks. To override a method, two things must happen:
  1. 1. The parent method must be marked open.
  1. 2. The child method must use the override keyword.
kotlin
123456789101112131415161718
open class Animal(val name: String) {
    // We explicitly allow this method to be overridden
    open fun makeSound() {
        println("Some generic animal sound...")
    }
}

class Cat(name: String) : Animal(name) {
    // We override the parent method
    override fun makeSound() {
        println("Meow!")
    }
}

fun main() {
    val myCat = Cat("Whiskers")
    myCat.makeSound() // Prints: Meow!
}

6. The super Keyword

What if you want to add behavior, but *also* keep the parent's original behavior? You can call the parent's version of the method using the super keyword.
kotlin
123456
class GuardDog(name: String) : Animal(name) {
    override fun makeSound() {
        super.makeSound() // Calls the Animal's generic sound first
        println("And a loud Bark!") // Then adds its own behavior
    }
}

7. Real-World Analogy: UI Components

In Android development, Button and TextView both inherit from a parent class called View. View handles drawing on the screen and width/height. Button inherits all of that, but overrides the click behavior!

8. Common Mistakes

  • Forgetting open: If you try to inherit from a standard class Animal, the compiler will throw: "This type is final, so it cannot be inherited from." You must add open.
  • Forgetting the parent constructor (): Writing class Dog : Animal is invalid. You must call the parent's constructor: class Dog : Animal().

9. Best Practices

  • Favor Composition over Inheritance: While Inheritance is powerful, don't overuse it. Deep inheritance trees (e.g., Dog inherits Mammal inherits Animal inherits Organism) become incredibly hard to maintain.

10. Exercises

  1. 1. Create an open class Vehicle with a method startEngine().
  1. 2. Create a class Car that inherits from Vehicle.
  1. 3. Override startEngine() in Car to print "Vroom!". Don't forget the open and override keywords.

11. MCQs with Answers

Question 1

What is the primary purpose of Inheritance in OOP?

Q2. By default, can you inherit from a class in Kotlin? a) Yes b) No, classes are final (closed) by default Answer: b) No, they are final by default.
Question 3

What keyword must be added to a class to allow other classes to inherit from it?

Question 4

What syntax is used to denote inheritance in Kotlin?

Question 5

If you want a child class to change the behavior of a parent's method, what must you do to the parent's method?

Question 6

What keyword is required on the child's method to successfully replace the parent's method?

Question 7

How do you call the parent class's version of a method from inside the child class?

Question 8

When inheriting, class Dog(name: String) : Animal(name) is valid. Why is (name) passed to Animal?

Q9. Is Multiple Inheritance (inheriting from two parent classes at once) allowed in Kotlin? a) Yes b) No, a class can only inherit from one parent class Answer: b) No.
Question 10

Why did Kotlin designers make classes final by default?

12. Interview Questions

  • Q: Why does Kotlin require the open keyword for inheritance and method overriding, whereas Java does not?
  • Q: Explain the purpose of the super keyword in an overridden method.

13. Summary

Inheritance allows us to model "Is-A" relationships (a Dog *is an* Animal). By grouping shared logic into an open parent class, our code becomes much more maintainable. Kotlin's strict requirement for the open and override keywords prevents developers from accidentally interfering with class behaviors, resulting in far more stable object hierarchies.

14. Next Chapter Recommendation

A class can only inherit from ONE parent class. What if our Dog needs behaviors from multiple sources, like being an Animal AND being a Pet? In Chapter 16: Interfaces and Abstract Classes, we will learn how to bypass single-inheritance limitations using Contracts.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·