Kotlin Constructor in Inheritance

In this article, you will learn about Constructors (both primary and secondary constructor) behavior in Inheritance, in Kotlin with the help of examples.

A constructor for a class is a special member function, which is called implicitly, just after the memory is allocated for the object to initialize the properties of the newly created object of that class type.

Using Inheritance, a subclass can inherit all the members (properties and functions) from its superclass (both those that are directly defined in the superclass and the ones that the superclass itself has inherited).

Constructors are not members, so they are not inherited by a subclass, but the constructor of the super class can be invoked from the sub class.

Creating Subclass Instance

When we create an object of a subclass, both subclass and superclass members get memory in subclass object.

The main motive of a constructor is to initialize the properties of a class. So both the constructor needs to be executed because this object (subclass) contains properties of both classes.

In Kotlin, the subclass must invoke one of the constructors (primary or secondary) of the base class, passing either parameters from its own constructor or constant values and the super class constructor is always called before the subclass constructor.

Kotlin Inheritance Primary Constructor 

If the subclass contains a primary constructor, then we have to initialize the base class constructor (primary or secondary) using the parameters of the sub class.

Subclass primary constructor calling Superclass primary constructor:

Example
//base class primary constructor with initializer block
open class Employee(name: String, age: Int) {
    init {
        println("My name is $name, $age years old  ")
    }
}

//derived class inheriting base class
//derived class primary constructor with initializer block
class Manager(name: String, age: Int, salary: Int) : Employee(name, age) {
    init {
        println("I am Manager and earning $salary per month.")
        println()
    }

}

fun main(args: Array<String>) {
//creating derived class instance
    Manager("Arun", 40, 90000)
}
Output
My name is Arun, 40 years old 
I am Manager and earning 90000 per month.
Example explained
  • When we create an object of Manager (sub class), Manager class primary constructor will call.
  • The Manager class local variables initialize with the respective values and pass the variable name and age as parameters to the Employee class.
  • The Employee class init block will execute first and then the Manager class init block will execute.

Kotlin Inheritance Secondary Constructor 

If the subclass does not contain primary constructor, then we have to call the superclass constructor (primary or secondary) from the secondary constructor of subclass using the super keyword. We also need to initialize the superclass secondary constructor using the parameters of subclass.

Calling parent primary constructor from child secondary constructor

Example
//base class primary constructor with initializer block
open class Employee(name: String, age: Int) {

    init {
        println("My name is $name, $age years old  ")
    }
}

//derived class inheriting base class
class Manager : Employee {
//secondary constructor calling base class primary constructor
    constructor(name: String, age: Int, salary: Int) : super(name, age) {
        println("I am Manager and earning $salary per month.")
        println()
    }

}

fun main(args: Array<String>) {
//creating derived class instance
    Manager("Arun", 40, 90000)

}
Output
My name is Arun, 40 years old 
I am Manager and earning 90000 per month.
Example explained
  • When we create an object of Manager (sub class), Manager class secondary constructor will call.
  • The Manager class (secondary constructor) local variables initialize with the respective values and pass the variable name and age as parameters to the Employee class.
  • The Employee class init block will execute first and then the remaining statements of secondary constructor will execute.

Calling parent secondary constructor from child secondary constructor

Example
//base class
open class Employee {

//base class secondary constructor
    constructor(name: String, age: Int) {
        println("My name is $name, $age years old  ")
    }
}

//derived class inheriting base class
class Manager : Employee {
//secondary constructor calling base class secondary constructor
    constructor(name: String, age: Int, salary: Int) : super(name, age) {
        println("I am Manager and earning $salary per month.")
        println()
    }

}

fun main(args: Array<String>) {
//creating derived class instance
    Manager("Arun", 40, 90000)
}
Output
My name is Arun, 40 years old 
I am Manager and earning 90000 per month.
Example explained
  • When we create an object of Manager (sub class), Manager class secondary constructor will call.
  • The Manager class(secondary constructor) local variables initialize with the respective values and pass the variable name and age as parameters to the Employee class.
  • The Employee class secondary constructor will execute first and then the remaining statements of Manager secondary constructor will execute.

Leave a Reply