Types of Inheritance in Kotlin

Kotlin supports several forms of inheritance that help organize code and reuse functionality. Unlike some languages, Kotlin does not allow multiple inheritance of classes but allows implementing multiple interfaces.

1. Single Inheritance

In single inheritance, a subclass inherits from only one parent class. This is the simplest form of inheritance.


open class Aircraft(val name: String) {
    fun status() = println("$name is ready for flight")
}

class Jet(name: String, val maxSpeed: Int) : Aircraft(name)

fun main() {
    val jet = Jet("Falcon", 1200)
    jet.status()  // Inherited from Aircraft
}
  

Output:


Falcon is ready for flight
  

2. Multilevel Inheritance

Multilevel inheritance occurs when a class inherits from a class that itself inherits from another class.


open class Aircraft(val name: String)

open class Jet(name: String, val maxSpeed: Int) : Aircraft(name)

class FighterJet(name: String, maxSpeed: Int, val missiles: Int) : Jet(name, maxSpeed)

fun main() {
    val fJet = FighterJet("Eagle", 1500, 8)
    println("${fJet.name} with ${fJet.missiles} missiles")
}
  

Output:


Eagle with 8 missiles
  

3. Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses inherit from a single parent class.


open class Aircraft(val name: String)

class Jet(name: String) : Aircraft(name)
class Helicopter(name: String) : Aircraft(name)

fun main() {
    val jet = Jet("Falcon")
    val heli = Helicopter("Apache")
    println(jet.name)
    println(heli.name)
}
  

Output:


Falcon
Apache
  

4. Interface-based / Multiple Inheritance of Behavior

Kotlin allows a class to implement multiple interfaces, which is how multiple inheritance of behavior is achieved without the diamond problem.


interface Flyable {
    fun fly() = println("Flying")
}

interface Movable {
    fun move() = println("Moving")
}

class Drone : Flyable, Movable

fun main() {
    val drone = Drone()
    drone.fly()   // From Flyable
    drone.move()  // From Movable
}
  

Output:


Flying
Moving
  

5. Why Multiple Inheritance of Classes is Not Allowed

If a class could inherit from multiple classes, it could create ambiguity in case the parent classes have methods with the same signature. This is called the diamond problem.


// Hypothetical problematic scenario
open class A {
    open fun action() = println("Action from A")
}

open class B {
    open fun action() = println("Action from B")
}

// class C : A(), B() // ❌ Error: multiple inheritance not allowed
  

Using interfaces, this problem is solved because Kotlin allows you to explicitly override methods and resolve conflicts.

6. Hybrid Inheritance

Hybrid inheritance is a combination of two or more inheritance types, such as multilevel, hierarchical, and interface-based inheritance. Kotlin supports hybrid inheritance safely using interfaces to implement multiple behaviors while maintaining single class inheritance.


open class Aircraft(val name: String) {
    fun status() = println("$name is ready")
}

// Multilevel + Hierarchical
class Jet(name: String, val maxSpeed: Int) : Aircraft(name)
class Helicopter(name: String) : Aircraft(name)

// Interface-based behaviors
interface Flyable {
    fun fly() = println("Flying")
}

interface Movable {
    fun move() = println("Moving")
}

// Hybrid Inheritance: class + multiple interfaces
class AdvancedDrone(name: String) : Aircraft(name), Flyable, Movable

fun main() {
    val drone = AdvancedDrone("X-Drone")
    drone.status() // from Aircraft
    drone.fly()    // from Flyable
    drone.move()   // from Movable
}
  

Output:


X-Drone is ready
Flying
Moving
  

Explanation:

7. Technical Interview Questions

8. Key Takeaways