kotlin
  1. kotlin-inheritance

Kotlin Inheritance

Inheritance is a mechanism in Kotlin by which one class inherits the properties and behaviors of another class. The class that inherits the properties is called the derived class, and the class that provides its properties is called the base class.

Syntax

To inherit from a base class in Kotlin, you can use the : operator followed by the name of the base class. For example:

class DerivedClass : BaseClass() {
    // code for the derived class goes here
}

In this example, the DerivedClass inherits from the BaseClass.

Example

Let's say we have a base class Vehicle with a property modelYear and a method drive(). We can create a derived class Car that inherits from Vehicle and has an additional property numDoors and a method startEngine():

open class Vehicle(val modelYear: Int) {
    fun drive() {
        println("Driving the vehicle")
    }
}

class Car(modelYear: Int, val numDoors: Int) : Vehicle(modelYear) {
    fun startEngine() {
        println("Starting the car engine")
    }
}

In this example, the Car class inherits from the Vehicle class and also has its own property numDoors and method startEngine().

Output

We can create an instance of the Car class and call its methods:

val myCar = Car(2020, 4)
println(myCar.modelYear) // Output: 2020
myCar.startEngine() // Output: Starting the car engine
myCar.drive() // Output: Driving the vehicle

Explanation

Inheritance allows a class to reuse code and functionality from another class without having to rewrite it. The derived class automatically inherits all public and protected properties and methods from the base class. You can also override methods from the base class in the derived class by providing a new implementation.

In the example above, the Car class automatically inherits the drive() method from the Vehicle class and also gets the modelYear property. The startEngine() method is specific to the Car class and is not available in the Vehicle class.

Use

Inheritance is useful when you want to create a new class that is similar to an existing class, but with some modifications or additions. It allows you to reuse code and avoid duplication. Inheritance is commonly used in object-oriented programming to create hierarchies of classes with increasing levels of specialization.

Important Points

  • In Kotlin, only one class can be inherited at a time (i.e., multiple inheritance is not allowed).
  • Base class constructors are always called before the derived class constructors.
  • You can use the super keyword to call methods and constructors from the base class.

Summary

Inheritance is a powerful mechanism in Kotlin that allows you to reuse code and create new classes based on existing ones. It is achieved using the : operator followed by the name of the base class. The derived class automatically inherits all public and protected properties and methods of the base class. You can also override methods from the base class in the derived class by providing a new implementation.

Published on: