Kotlin Inheritance & Polymorphism

Kotlin Inheritance & Polymorphism (Deep Explanation)

Kotlin supports inheritance and polymorphism with strict rules that make code safe, explicit, and less error-prone than Java.


PART 1: INHERITANCE IN KOTLIN

1. What is Inheritance?

Inheritance allows a child class to reuse properties and functions of a parent class.


2. open Keyword (Very Important)

In Kotlin:

  • Classes are final by default

  • Functions are final by default

You must use open to allow inheritance.

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

3. Creating a Child Class

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

4. Overriding Functions

Parent function must be open, child must use override.

open class Animal {
open fun sound() {
println("Animal sound")
}
}

class Dog : Animal() {
override fun sound() {
println("Bark")
}
}


5. Overriding Properties

open class Animal {
open val type = "Animal"
}

class Dog : Animal() {
override val type = "Dog"
}

Rules:

  • val → can be overridden by val or var

  • var → can be overridden only by var


6. Calling Parent Implementation (super)

open class Animal {
open fun sound() {
println("Animal sound")
}
}

class Dog : Animal() {
override fun sound() {
super.sound()
println("Bark")
}
}


7. Primary Constructor in Inheritance

open class Animal(val name: String)

class Dog(name: String) : Animal(name)


8. Preventing Override (final)

open class Animal {
final fun breathe() {
println("Breathing")
}
}

Child cannot override breathe().


PART 2: POLYMORPHISM IN KOTLIN


9. What is Polymorphism?

Polymorphism means:

One reference, different behavior


10. Runtime Polymorphism (Dynamic Dispatch)

val animal: Animal = Dog()
animal.sound() // Bark

Decision happens at runtime.


11. Compile-Time Polymorphism (Overloading)

class Printer {
fun print(value: String) {
println(value)
}

fun print(value: Int) {
println(value)
}
}


12. Abstract Classes (Inheritance + Polymorphism)

abstract class Shape {
abstract fun area(): Double
}
class Circle(val r: Double) : Shape() {
override fun area() = 3.14 * r * r
}

13. Interfaces & Polymorphism

interface Flyable {
fun fly()
}
class Bird : Flyable {
override fun fly() {
println("Flying")
}
}

14. Multiple Inheritance (Interfaces Only)

Kotlin does NOT allow multiple class inheritance
But supports multiple interfaces.

interface A {
fun show() {
println("A")
}
}

interface B {
fun show() {
println("B")
}
}

class C : A, B {
override fun show() {
super<A>.show()
super<B>.show()
}
}


15. Smart Cast & Polymorphism

fun makeSound(animal: Animal) {
if (animal is Dog) {
animal.bark() // smart cast
}
}

16. Sealed Classes (Restricted Polymorphism)

sealed class Result {
data class Success(val data: String) : Result()
object Error : Result()
}

Used with when:

when (result) {
is Result.Success -> println(result.data)
Result.Error -> println("Error")
}

17. Real-World Example

open class Payment {
open fun pay() {
println("Processing payment")
}
}

class CardPayment : Payment() {
override fun pay() {
println("Paid by card")
}
}

class UpiPayment : Payment() {
override fun pay() {
println("Paid by UPI")
}
}

fun process(payment: Payment) {
payment.pay()
}

Summary (Key Rules)

open required for inheritance
override is mandatory
✔ Polymorphism works via parent references
✔ Use abstract & sealed for controlled hierarchies
✔ Interfaces support multiple inheritance

You may also like...