<p>In this article, you will learn about Constructors (both primary and secondary constructor) behavior in Inheritance, in Kotlin with the help of examples.</p>
<p>A <span style="color: #0000ff;"><strong>constructor</strong></span> 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.</p>
<p>Using Inheritance, a <span style="color: #0000ff;"><strong>subclass</strong></span> can inherit all the members (properties and functions) from its <span style="color: #0000ff;"><strong>superclass</strong></span> (both those that are directly defined in the superclass and the ones that the superclass itself has inherited).</p>
<p><strong><span style="color: #008000;">Constructors</span> </strong>are not members, so they are not inherited by a subclass, but <span style="color: #0000ff;"><strong>the constructor of the super class can be invoked from the sub class</strong>.</span></p>
<h3><span style="color: #000080;"><strong>Creating Subclass Instance</strong></span></h3>
<p>When we create an object of a subclass, both subclass and superclass members get memory in <strong><span style="color: #008000;">subclass</span></strong> object.</p>
<p>The main motive of a <span style="color: #008000;"><strong>constructor</strong></span> 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.</p>
<p class="p1">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.</p>
<h3 class="p1"><span style="color: #000080;"><b>Kotlin Inheritance Primary Constructor </b></span></h3>
<p class="p2">If the subclass contains a <span style="color: #008000;"><strong>primary</strong></span> constructor, then we have to initialize the base class constructor (primary or secondary) using the parameters of the sub class.</p>
<h4><span style="color: #000080;"><strong>Subclass primary constructor calling Superclass primary constructor:</strong></span></h4>
<h5><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">//base class primary constructor with initializer block</span>
open</strong></span> class Employee(name: String, age: Int) {
 init {
 println("My name is $name, $age years old ")
 }
}

<strong><span style="color: #008000;">//derived class inheriting base class</span></strong>
<span style="color: #008000;"><strong>//derived class primary constructor with initializer block</strong></span>
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>;) {
<strong><span style="color: #008000;">//creating derived class instance</span></strong>
 Manager("Arun", 40, 90000)
}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>My name is Arun, 40 years old 
I am Manager and earning 90000 per month.</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li>When we create an object of <strong><span style="color: #008000;">Manager</span></strong> (sub class), Manager class <strong><span style="color: #008000;">primary</span></strong> constructor will call.</li>
<li>The Manager class local variables initialize with the respective values and pass the variable <strong><span style="color: #0000ff;">name</span></strong> and <span style="color: #0000ff;"><strong>age</strong></span> as parameters to the <strong><span style="color: #008000;">Employee</span></strong> class.</li>
<li>The Employee class <strong><span style="color: #008000;">init</span></strong> block will execute first and then the Manager class <strong><span style="color: #008000;">init</span></strong> block will execute.</li>
</ul>
<h3 class="p1"><span style="color: #000080;"><b>Kotlin Inheritance Secondary Constructor </b></span></h3>
<p class="p2">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 <strong><span style="color: #0000ff;">super</span></strong> keyword. We also need to initialize the superclass secondary constructor using the parameters of subclass.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h4><span style="color: #000080;"><strong>Calling parent primary constructor from child secondary constructor</strong></span></h4>
<h5><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><strong><span style="color: #008000;">//base class primary constructor with initializer block</span></strong>
open class Employee(name: String, age: Int) {

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

<strong><span style="color: #008000;">//derived class inheriting base class</span></strong>
class Manager : Employee {
<strong><span style="color: #008000;">//secondary constructor calling base class primary constructor</span></strong>
 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>;) {
<strong><span style="color: #008000;">//creating derived class instance</span></strong>
 Manager("Arun", 40, 90000)

}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>My name is Arun, 40 years old 
I am Manager and earning 90000 per month.</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li>When we create an object of <strong><span style="color: #008000;">Manager</span></strong> (sub class), Manager class <span style="color: #008000;"><strong>secondary</strong></span> constructor will call.</li>
<li>The Manager class (secondary constructor) local variables initialize with the respective values and pass the variable <strong><span style="color: #0000ff;">name</span></strong> and <span style="color: #0000ff;"><strong>age</strong></span> as parameters to the <strong><span style="color: #008000;">Employee</span></strong> class.</li>
<li>The Employee class <strong><span style="color: #008000;">init</span></strong> block will execute first and then the remaining statements of secondary constructor will execute.</li>
</ul>
<h4></h4>
<h4><span style="color: #000080;"><strong>Calling parent secondary constructor from child secondary constructor</strong></span></h4>
<h5><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><strong><span style="color: #008000;">//base class</span></strong>
open class Employee {

<strong><span style="color: #008000;">//base class secondary constructor</span></strong>
 constructor(name: String, age: Int) {
 println("My name is $name, $age years old ")
 }
}

<strong><span style="color: #008000;">//derived class inheriting base class</span></strong>
class Manager : Employee {
<strong><span style="color: #008000;">//secondary constructor calling base class secondary constructor</span></strong>
 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>;) {
<strong><span style="color: #008000;">//creating derived class instance</span></strong>
 Manager("Arun", 40, 90000)
}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>My name is Arun, 40 years old 
I am Manager and earning 90000 per month.</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li>When we create an object of <strong><span style="color: #008000;">Manager</span></strong> (sub class), Manager class <strong><span style="color: #008000;">secondary</span></strong> constructor will call.</li>
<li>The Manager class(secondary constructor) local variables initialize with the respective values and pass the variable <strong><span style="color: #0000ff;">name</span></strong> and <span style="color: #0000ff;"><strong>age</strong></span> as parameters to the <strong><span style="color: #008000;">Employee</span></strong> class.</li>
<li>The Employee class secondary constructor<strong><span style="color: #000000;"> </span></strong>will execute first and then the remaining statements of Manager secondary constructor will execute.</li>
</ul>


