KOTLIN Tutorial



INHERITANCE IN KOTLIN


Inheritance in Kotlin

Inheritance is one of the core concepts in Object-Oriented Programming (OOP). It allows a class to inherit properties and functions from another class, enabling code reuse and extension. Kotlin supports single inheritance, where a class can inherit from only one base class.

Let’s dive into how inheritance works in Kotlin!


What is Inheritance?

In Kotlin, a class can inherit properties and functions from another class. The class that is inherited from is called the superclass, and the class that inherits from it is called the subclass.

To inherit a class in Kotlin, you use the : symbol. The subclass will automatically inherit the constructor and properties of the superclass unless you override them.

Example of Inheritance

open class Animal(val name: String) {
    fun speak() {
        println("$name makes a sound")
    }
}

class Dog(name: String) : Animal(name) {
    fun bark() {
        println("$name barks!")
    }
}

fun main() {
    val dog = Dog("Buddy")
    dog.speak() // Inherited function
    dog.bark()  // Function specific to Dog
}
    

In the example above:

  • The Animal class is the superclass with a speak method.
  • The Dog class is the subclass that inherits from Animal and adds its own bark method.
  • The subclass can use both inherited and its own methods.

Overriding Methods in Kotlin

Sometimes, you may want to modify or replace a method in the superclass. This can be done using the override keyword.

Overriding a Function

open class Animal(val name: String) {
    open fun speak() {
        println("$name makes a sound")
    }
}

class Dog(name: String) : Animal(name) {
    override fun speak() {
        println("$name barks loudly!")
    }
}

fun main() {
    val dog = Dog("Buddy")
    dog.speak() // Overridden function
}
    

In this example:

  • The speak method in Animal is overridden by the Dog class.
  • The override keyword ensures that you are intentionally replacing the superclass method.

Accessing Superclass Methods

If you want to call the method of the superclass within the subclass, you can use the super keyword.

Using `super` to Call Superclass Methods

open class Animal(val name: String) {
    open fun speak() {
        println("$name makes a sound")
    }
}

class Dog(name: String) : Animal(name) {
    override fun speak() {
        super.speak() // Calling superclass method
        println("$name barks loudly!")
    }
}

fun main() {
    val dog = Dog("Buddy")
    dog.speak() // Calls both superclass and subclass method
}
    

In this example, the super.speak() call invokes the original speak method from the Animal class before executing the subclass's behavior.


Abstract Classes in Kotlin

An abstract class cannot be instantiated on its own and is meant to be inherited by other classes. Abstract methods in an abstract class must be overridden in the subclass.

Using Abstract Classes

abstract class Animal(val name: String) {
    abstract fun speak() // Abstract method
}

class Dog(name: String) : Animal(name) {
    override fun speak() {
        println("$name barks loudly!")
    }
}

fun main() {
    val dog = Dog("Buddy")
    dog.speak() // Calls overridden function
}
    

Here:

  • The Animal class is abstract and contains an abstract method speak.
  • The Dog class inherits from Animal and must override the speak method.

πŸ§ͺ Try It Yourself

Here are some challenges to get hands-on experience with inheritance:

  • Create a Shape class with an abstract method area(). Create subclasses like Circle and Rectangle, each implementing the area() method to calculate its area.
  • Write an abstract class Person with properties like name and age. Create subclasses like Student and Teacher, where each subclass overrides a method called role().
  • Create a Vehicle class with a method startEngine(). Inherit it in subclasses like Car and Truck and override startEngine() in each subclass with unique behavior.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review