In this article, I am explaining how you can reuse your existing code of Kotlin using inheritance, with examples and diagram.
Inheritance
Inheritance is used to access the properties of the base class to a derived class. It is the best approach for code reusability in applications from the development point of view. In Kotlin, you can use ‘:’ colon operator for inheritance purpose.
Kotlin has a class ‘Any’ which is the superclass of all classes and it does not have any other parent class. The ’Any’ class has only three members - ‘equals()’, ’hascode()’, and ‘toString()’.
Single Inheritance
Syntax
- class <childclass > : <Parentclass>(){
- }
In Kotlin, everything is final by default. So, if you want to create a parent class, then you have to use ‘open’ keyword before the name of the parent class.
Example- open class AccountInfo(var name:String,var number:Int){
- fun publicInfo(){
- println("A/C Name=$name")
- println("A/C Number=$number")
- }
- }
- class PersonalInfo:AccountInfo("Mani",101005){
- var email:String="[email protected]"
- var contact:Long=8882265032L
- fun showContactInfo(){
- println("Email=$email")
- println("Contact=$contact")
- }
- }
- fun main(args:Array<String>){
- var pi=PersonalInfo()
- pi.publicInfo()
- pi.showContactInfo()
- }
-
-
-
-
Explanation
In the above example, there is a parent class ‘AccountInfo’ which has two data members and one member method. Now, the subclass ‘PersonalInfo’ is extending its parent class ‘AccountInfo’ using ‘:’ operator. Now, the object of ‘PersonalInfo‘ class is able to access the members of parent class.
Multilevel Inheritance
When a derived class is again inherited by a child class, it creates multilevel inheritance.
Example
- open class Employee(var empid:Int){
- fun showId(){
- println("Employee Id="+empid)
- }
- }
- open class AddressInfo(var address:String,var id:Int):Employee(id){
- fun showAddress(){
- println("Address=$address")
- }
- }
- open class ContactsInfo(var number:Long,var add:String,var userid:Int):AddressInfo(add,userid){
- fun showContacts(){
- println("Contact=$number")
- }
- }
- fun main(args:Array<String>){
- var info=ContactsInfo(8882265032,"Delhi",5001)
- info.showId()
- info.showAddress()
- info.showContacts()
- }
-
-
-
-
-
In the above example, there is a parent class ‘Employee’ which has been inherited by its child class ‘AddressInfo’ and this child class is inherited by ‘ContactsInfo’ child class. So Inheritance is being performed at more than one levels, called multilevel inheritance. Because of these features, the object of ‘ContactsInfo’ class is capable to access the 'showId()’ function from ‘Employee’ parent class,’showAddress()’ from ‘AddressInfo’, and ‘showContacts()’ from ‘ContactsInfo’ class.
Multiple Inheritance
When a class inherits more than one parent classes, it is known as multiple inheritance. Like Java, Kotlin does not support multiple inheritance .But we can do operations like multiple inheritances using interface in Kotlin. So, we can implement more than one interfaces in a single class by using ‘:’ operator.
Example
Let us understand this with an example .
- interface Cars {
- fun carsspeed()
- }
- interface Bikes {
- fun bikesspeed()
- }
- class Vichel : Cars,Bikes{
- override fun carsspeed() {
- println("Car is running on 100 km/hrs")
- }
-
- override fun bikesspeed() {
- println("Bike is running on 60 km/hrs")
- }
- }
- fun main(args:Array<String>){
- var vichel=Vichel()
- vichel.bikesspeed()
- vichel.carsspeed()
- }
-
-
-
-
Explanation
In the above example, there are two interfaces ‘Cars’ and ‘Bikes’ which have two methods ‘carsspeed()’ and ‘bikesspeed()’ respectively. These interfaces are being implemented by a class ‘Vichel’ which makes you able to use methods of interfaces.
ConclusionThis article explained the inheritance concept in Kotlin including single, multilevel, and multiple. By understanding these, I hope it is possible to implement another more types of inheritance like hybrid and hierarchical .Please go through my previous articles to be familiar with Kotlin and follow me to get notification for upcoming articles.