Object-Oriented Programming (OOP) in Kotlin
Estimated reading: 4 minutes 26 views

🧬 Kotlin – Inheritance: Reuse and Extend Class Functionality

🧲 Introduction – Why Learn Kotlin Inheritance?

Inheritance allows you to reuse code and extend class behavior in a clean and structured way. Kotlin enhances classic OOP inheritance with explicit control, meaning classes and functions are final by default. To allow inheritance, you must explicitly mark classes or methods with the open keyword.

🎯 In this guide, you’ll learn:

  • How inheritance works in Kotlin
  • Use of open, override, and super
  • Constructor handling in base and derived classes
  • Best practices for designing an inheritance-friendly class hierarchy

👨‍👧 What Is Inheritance in Kotlin?

Inheritance is an object-oriented concept where one class inherits properties and behaviors from another class.

  • Base class (superclass) → Defines common features
  • Derived class (subclass) → Inherits and extends the base class

🏗️ Declaring an Inheritable Class with open

By default, Kotlin classes are not inheritable. Use open to enable inheritance.

open class Animal {
    fun eat() = println("Eating food")
}

🧱 Creating a Subclass with :

class Dog : Animal() {
    fun bark() = println("Barking...")
}

🔹 Usage:

val dog = Dog()
dog.eat()
dog.bark()

🟢 Output:

Eating food  
Barking...

✔️ The Dog class can use both its own method (bark) and the inherited one (eat).


🔁 Overriding a Method with override

To override a function, it must be open in the base class.

open class Vehicle {
    open fun start() = println("Vehicle starting...")
}

class Car : Vehicle() {
    override fun start() = println("Car started")
}
val car = Car()
car.start()

🟢 Output:

Car started

✔️ override must match the function signature and is mandatory for safe override handling.


🧩 Calling the Superclass Function – super

You can call a method from the base class using super.

open class Parent {
    open fun greet() = println("Hello from Parent")
}

class Child : Parent() {
    override fun greet() {
        super.greet()
        println("Hello from Child")
    }
}

🟢 Output:

Hello from Parent  
Hello from Child

🧱 Inheriting Constructors

You can pass constructor parameters from subclass to superclass using : baseClass(args):

open class Person(val name: String)

class Student(name: String, val grade: Int) : Person(name)

✔️ Primary constructors must be called from the subclass header.


🧠 Inheritance + Polymorphism

open class Shape {
    open fun draw() = println("Drawing Shape")
}

class Circle : Shape() {
    override fun draw() = println("Drawing Circle")
}

val shape: Shape = Circle()
shape.draw()

🟢 Output:

Drawing Circle

✔️ Polymorphism allows the same function to behave differently based on the object type.


🚫 Common Mistakes

❌ Mistake✅ Fix
Forgetting to mark class openAdd open to base class or method
Overriding final methodOnly open methods can be overridden
Not calling base constructorUse : super(args) or : BaseClass(args)
Relying too heavily on inheritancePrefer composition or interfaces when possible

✅ Best Practices for Kotlin Inheritance

PracticeWhy It Helps
Use open and override clearlyEnsures readable and safe overrides
Prefer composition over inheritanceKeeps design flexible and reduces tight coupling
Keep base classes smallEasier to maintain and reuse
Encapsulate reusable logicPromotes DRY (Don’t Repeat Yourself) principles

📌 Summary – Recap & Next Steps

Kotlin inheritance enables you to reuse and override behavior across a class hierarchy. While Kotlin makes inheritance safe by requiring explicit keywords, it remains a powerful tool when used wisely.

🔍 Key Takeaways:

  • Kotlin classes are final by default—use open to allow inheritance
  • Use override to redefine behavior, and super to access parent logic
  • Constructors in subclasses must call base constructors
  • Inheritance enables code reuse, specialization, and polymorphism

⚙️ Practical Use:
Inheritance is widely used in Android development, custom views, data modeling, and framework extension.


❓ FAQs – Kotlin Inheritance

Why are classes final by default in Kotlin?
✅ To enforce safer and more predictable code. You must explicitly use open to allow inheritance.


What is the difference between open and override?
open allows a method or class to be overridden, and override is used in the subclass to implement the override.


How do I inherit from a class with a constructor?
✅ Use subclass constructor to pass arguments:

class Student(name: String) : Person(name)

Can I call the parent method in an overridden function?
✅ Yes. Use super.methodName() to call the superclass method.


Is multiple inheritance supported in Kotlin?
✅ Kotlin doesn’t support multiple class inheritance but supports multiple interface inheritance.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin – Inheritance

Or Copy Link

CONTENTS
Scroll to Top