Inheritance is an important feature of object oriented programming language. Inheritance allows to inherit the feature of existing class (or base or parent class) to new class (or derived class or child class).
The main class is called super class (or parent class) and the class which inherits the superclass is called subclass (or child class). The subclass contains features of superclass as well as its own.
The concept of inheritance is allowed when two or more classes have same properties. It allows code reusability. A derived class has only one base class but may have multiple interfaces whereas a base class may have one or more derived classes.
In Kotlin, the derived class inherits a base class using: operator in the class header (after the derive class name or constructor)
Suppose that,we have two different classes "Programmer" and "Salesman" having the common properties 'name','age', and 'salary' as well as their own separate functionalitiesdoProgram() and fieldWork(). The feature of inheritance allows that we can inherit (Employee) containing the common features.
All Kotlin classes have a common superclass "Any". It is a default superclass for a class with no supertypes explicitly specified.
For example, a class Example is implicitly inherited from Any.
Kotlin open keyword
As Kotlin classes are final by default, they cannot be inherited simply. We use the open keyword before the class to inherit a class and make it to non-final,
Kotlin Inheriting fields from a class
When we inherit a class to derive class, all the fields and functionalities are inherited. We can use these fields and functionalities in derived class.
x is equal to 10
Kotlin Inheriting methods from a class
Kotlin Inheritance Example
Here, we declare a class Employee is superclass and Programmer and Salesman are their subclasses. The subclasses inherit properties name, age and salary as well as subclasses containtheir own functionalitieslike doProgram() and fieldWork().
Name is Ashu. Age is 25 Salary is 40000.0 programming is my passion. Name is Ajay. Age is 24 Salary is 30000.0 travelling is my hobby.
Kotlin Inheritance and primary constructor
If the base and derived class both having primary constructor in that case the parameters are initialized in the primary constructor of base class. In above example of inheritance, all classes contain three parameters "name", "age" and "salary" and all these parameters are initialized in primary constructor of base class.
When a base and derived class both contains different numbers of parameters in their primary constructor then base class parameters are initialized form derived class object.
Name is Ashu. Salary is 40000.0 Name Ashu of department Development with salary 40000.0. Programming is my passion. Name is Ajay. Salary is 30000.0 Name Ajay of department Marketing with salary 30000.0. Travelling is my hobby.
When an object of derived class is created, it calls its superclass first and executes init block of base class followed by its own.
Kotlin Inheritance and secondary constructor
If derived class does not contain any primary constructor then it is required to call the base class secondary constructor from derived class using super keyword.
execute super constructor Ashu: 101 execute child class constructor with property Ashu, 101, Developer
In above example, when object of Child class is created, it calls its constructor and initializes its parameters with values "Ashu", "101" and "Developer". At the same time Child class constructor calling its supper class constructor using super keyword with values of name and id. Due to the presence of super keyword thebody of superclass constructor executes first and returns to Child class constructor.
Kotlin Method Overriding
Method overriding means providing the specific implementation of method of super (parent) class into its subclass (child) class.
In other words, when subclass redefines or modifies the method of its superclass into subclass, it is known as method overriding. Method overriding is only possible in inheritance.
KotlinRules of method overriding
Example of inheritance without overriding
Bird is flying... Bird is flying...
In above example, a program without overriding the method of base class we found that both derived classes Parrot and Duck perform the same common operation. To overcome with this problem we use the concept of method overriding.
Example of Kotlin method overriding
In this example, the method fly() of parent class Bird is overridden in its subclass Parrot and Duck. To override the method of parent class, the parent class and its method which is going to override must be declare as open. At the same time method which is overridden in child class must be prefaced with keyword override.
Parrot is flying... Duck is flying...
Example of Kotlin property overriding
Property of superclass can also be overridden in its subclass as similar to method. A color property of Bird class is overridden in its subclass Parrot and Duck and modified.
Parrot is flying... Green Duck is flying... White
We can override the val property with var property in inheritance but vice-versa is not true.
Kotlin superclass implementation
Derived class can also call its superclass methods and property using super keyword.
Bird is flying... Parrot is flying... Green
Kotlin multiple class implementation
In Kotlin, derived class uses a supertype name in angle brackets, e.gsuper<Base> when it implements same function name provided in multiple classes.
For example, a derived class Parrotextends its superclass Bird and implement Duck interface containing same function fly(). To call particular method of each class and interface we must be mention supertype name in angle brackets as super<Bird>.fly() and super<Duck>.fly() for each method.
Bird is flying... Duck is flying... Parrot is flying...