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 withWarrior 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
openkeyword to allow inheritance.
-
Inherit properties and methods using the
:syntax.
-
Override parent methods using the
overridekeyword.
-
Call parent logic using the
superkeyword.
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
4. Creating a Subclass
To inherit from a parent class, we use a colon: followed by the parent class name and its constructor.
kotlin
5. Method Overriding
Sometimes a child class wants to change how a parent method works. For example, a genericAnimal might make a sound, but a Dog barks.
To override a method, two things must happen:
-
1.
The parent method must be marked
open.
-
2.
The child method must use the
overridekeyword.
kotlin
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
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 standardclass Animal, the compiler will throw: "This type is final, so it cannot be inherited from." You must addopen.
-
Forgetting the parent constructor
(): Writingclass Dog : Animalis 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.,
DoginheritsMammalinheritsAnimalinheritsOrganism) become incredibly hard to maintain.
10. Exercises
-
1.
Create an
open class Vehiclewith a methodstartEngine().
-
2.
Create a
class Carthat inherits fromVehicle.
-
3.
Override
startEngine()inCarto print "Vroom!". Don't forget theopenandoverridekeywords.
11. MCQs with Answers
Question 1
What is the primary purpose of Inheritance in OOP?
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?
Question 10
Why did Kotlin designers make classes final by default?
12. Interview Questions
-
Q: Why does Kotlin require the
openkeyword for inheritance and method overriding, whereas Java does not?
-
Q: Explain the purpose of the
superkeyword in an overridden method.
13. Summary
Inheritance allows us to model "Is-A" relationships (a Dog *is an* Animal). By grouping shared logic into anopen 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 ourDog 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.