<p>In this tutorial, we will learn about Inheritance in Kotlin with the help of examples.</p>
<h3 class="p1"><span style="color: #000080;"><strong>Inheritance</strong></span></h3>
<p class="p1"><span style="color: #0000ff;"><b>Inheritance </b></span>is one of the key feature of <b><span style="color: #0000ff;">OOP</span> (Object Oriented Programming)</b>. <b><span style="color: #008000;">Inheritance</span> </b>can be defined as the process where one class is allowed to inherit the features (properties and functions) of another class.</p>
<p class="p1">The class which inherits the features of other class is known as <b><span style="color: #0000ff;">sub class</span> </b>(or a derived class or a child class).</p>
<p class="p1">The class whose features are inherited is known as <span style="color: #0000ff;"><b>super class</b></span> ( or a base class or a parent class).</p>
<p>Inheritance supports the concept of <span style="color: #0000ff;">“<b>reusability</b>”</span>, i.e. the code that is present in the parent class doesn’t need to be written again in the child class.</p>
<p class="p1">The <span style="color: #008000;"><strong>subclass</strong></span> inherits all members that exist in its <span style="color: #008000;"><strong>superclass</strong></span> (both those that are directly defined in the superclass and the ones that the superclass itself has inherited) and can add some members of their own.</p>
<h5><strong><span style="color: #0000ff;">Syntax of Inheritance</span></strong></h5>
<pre><span style="color: #0000ff;"><strong>open</strong></span> class Superclass {
 <strong><span style="color: #008000;">//properties and functions</span></strong> 
}

class Subclass<span style="color: #0000ff;"> <strong>:</strong></span> Superclass()
{
 <strong><span style="color: #008000;">//properties and functions</span></strong>
}</pre>
<p class="p1">In Kotlin,</p>
<ul>
<li class="p1">All classes are <span style="color: #0000ff;"><b>final</b></span> by default (final class cannot be subclassed) . To inherit, super class for sub class we should use <span style="color: #0000ff;"><b>open</b></span> keyword in front of super class.</li>
<li class="p1">The sub class inherits a super class using <strong>:</strong> operator in the class header (after the sub class name or constructor).</li>
</ul>
<p class="p1">When we inherit a class then all the properties and functions are also inherited. We can use the super class properties and functions in the sub class and can also call functions using the sub class object.</p>
<h5><strong><span style="color: #0000ff;">Inheritance Example</span></strong></h5>
<pre><strong><span style="color: #008000;">//Calculator is a super class</span>
<span style="color: #0000ff;">open</span></strong> class <strong><span style="color: #0000ff;">Calculator</span></strong> {
 var num = 9

 <strong><span style="color: #008000;">//Calculator class members</span></strong>
 fun add(x: Int, y: Int): Int {
 return x + y
 }

 fun sub(x: Int, y: Int): Int {
 return x - y
 }
}

<strong><span style="color: #008000;">//sub class CalcAdv inherit Calculator</span></strong>
class <strong><span style="color: #0000ff;">CalcAdv</span></strong> : Calculator() {

 <span style="color: #008000;"><strong> //CalcAdv class members</strong></span>
 fun sqr(): Int {
 return num * num <span style="color: #008000;"><strong>//inherit num property</strong></span>
 }

 fun sqrt(): Double {
 return Math.sqrt(num.toDouble()) <span style="color: #008000;"><strong> //inherit num property</strong></span>
 }
}

fun main(args: Array<;String>;) {

 <strong><span style="color: #008000;">//creating CalcAdv instance</span></strong>
 val calcAdv = CalcAdv()

 <strong><span style="color: #008000;"> // calling add() and sub(), which are inherited from Calculator</span></strong>
 println("The sum of the given numbers: " + calcAdv.add(10, 20))
 println("The difference between the given numbers: " + calcAdv.sub(30, 20))


 <strong><span style="color: #008000;">// calling sqr() and sqrt(), which are CalcAdv's own functions</span></strong>
 println("The square of a given number: " + calcAdv.sqr())
 println("The square root of a given number: " + calcAdv.sqrt())
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>The sum of the given numbers: 30
The difference between the given numbers: 10
The square of a given number: 81
The square root of a given number: 3.0</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<p>In the above example,</p>
<ul class="ul1">
<li class="li2">We have inherited a subclass <span style="color: #008000;"><b>CalcAdv</b></span> from super class <span style="color: #008000;"><b>Calculator</b></span>.</li>
<li class="li2">The CalcAdv class inherits the property <span style="color: #008000;"><strong>num</strong></span> and the functions <span style="color: #008000;"><b>add()</b></span> and <span style="color: #008000;"><b>sub()</b></span> from the Calculator class.</li>
<li class="li2">When an object of <span style="color: #008000;"><b>CalcAdv</b></span> class is created, a copy of all functions and properties of the super class acquire memory in this object. That is why by using the object of the subclass we can also access the members of a super class.</li>
</ul>
<p> ;</p>
<h3 class="p1"><span style="color: #000080;"><strong>Overriding member functions and properties</strong></span></h3>
<p class="p1">Just like Kotlin classes, members (properties and functions) of a Kotlin class are also <span style="color: #0000ff;"><strong>final</strong></span> by default. To allow a member function to be overridden (redefine or modify the method of its superclass into subclass), you need to mark it with the <span style="color: #0000ff;"><strong>open</strong></span> modifier.</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>

<p class="p1">Moreover, the sub class that overrides a super class function must use the <span style="color: #0000ff;"><strong>override</strong></span> modifier.</p>
<h5 class="p1"><span style="color: #0000ff;"><b>Kotlin program of overriding the member function:</b></span></h5>
<pre><span style="color: #008000;"><strong>//Employee is a super class</strong></span>
<span style="color: #0000ff;"><strong>open</strong></span> class <span style="color: #000000;"><strong>Employee</strong></span> {

 <strong><span style="color: #0000ff;">open</span></strong> fun show(name: String, age: Int) {
 println("My name is $name, $age years old ")
 }
}

<span style="color: #008000;"><strong>//sub class Manager inherit Employee</strong></span>
class <span style="color: #000000;"><strong>Manager</strong></span> : Employee() {

<span style="color: #008000;"><strong> //overriding show() to provide new implementation</strong></span>
 <span style="color: #0000ff;"><strong>override</strong></span> fun show(name: String, age: Int) {
 println("My name is $name ")
 println("I am Manager in HCL company")
 }
}

fun main(args: Array<;String>;) {
<strong><span style="color: #008000;"> //creating Manager instance</span></strong>
 val manager = Manager()
<strong><span style="color: #008000;"> //show() of Manager class will call</span></strong>
 manager.show("Arun", 25)

}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>My name is Arun 
I am Manager in HCL company</pre>
<p class="p1">Similarly, we can override the <span style="color: #0000ff;"><strong>property</strong></span> of super class in sub class.</p>
<h5 class="p1"><span style="color: #0000ff;"><b>Kotlin program of overriding the member property :</b></span></h5>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">//Employee is a super class</span>
open</strong></span> class <span style="color: #000000;"><strong>Employee</strong></span> {
 <strong><span style="color: #0000ff;">open</span></strong> var position: String = "Worker"
 <span style="color: #0000ff;"><strong>open</strong></span> var salary = "20000"

}

<strong><span style="color: #008000;">// sub class Manager inherit Employee</span></strong>
class <strong><span style="color: #000000;">Manager</span></strong> : Employee() {
<strong><span style="color: #008000;">//overriding properties to set new values</span></strong>
 <strong><span style="color: #0000ff;">override</span></strong> var position: String = "Manager"
 <strong><span style="color: #0000ff;">override</span></strong> var salary = "70000"
}

fun main(args: Array<;String>;) {
<strong><span style="color: #008000;">//creating Manager instance</span></strong>
 val m = Manager()
 println("I am " + <strong>m.position</strong> + " and earning " + <strong>m.salary</strong> + " per month.")
}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>I am Manager and earning 70000 per month.</pre>
<p> ;</p>
<h3 class="p1"><span style="color: #000080;"><strong>Calling properties and functions of superclass </strong></span></h3>
<p class="p1">When you override a property or a member function of a super class, the super class implementation is shadowed by the child class implementation.</p>
<p class="p1">You can access super class member functions or properties from the sub class using the <span style="color: #0000ff;"><strong>super</strong></span> keyword.</p>
<p class="p1">In the below program we have called the super class property <span style="color: #008000;"><strong>color</strong></span> and function <span style="color: #008000;"><strong>draw()</strong></span> in sub class using the <span style="color: #0000ff;"><strong>super</strong></span> keyword.</p>
<h5><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">//Shape is a super class</span>
open</strong></span> class Shape {

 val color: String = "blue"

 <strong><span style="color: #0000ff;">open</span></strong> fun draw() {
 println("Drawing a shape..")
 }

}

<strong><span style="color: #008000;">//sub class Rectangle inherit Shape</span></strong> 
class Rectangle : Shape() {

 <strong><span style="color: #0000ff;">override</span></strong> fun draw() {
<strong><span style="color: #008000;"> //calling Shape class draw() and 
 //color property using super keyword</span></strong>
 <strong><span style="color: #0000ff;">super.draw()</span></strong>
 println("Filling the rectangle with " + <strong><span style="color: #0000ff;">super.color</span></strong> + " color")
 }

}

fun main(args: Array<;String>;) {
<strong><span style="color: #008000;"> //creating Rectangle instance</span></strong>
 val rectangle = Rectangle()
<span style="color: #008000;"><strong> //calling Rectangle class draw()</strong></span>
 rectangle.draw()
}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>Drawing a shape..
Filling the rectangle with blue color</pre>
<p> ;</p>


