Code Reusability In Kotlin

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

kotlin
Syntax

  1. class <childclass > : <Parentclass>(){  
  2. }   
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
  1.  open class AccountInfo(var name:String,var number:Int){  
  2.     fun publicInfo(){  
  3.         println("A/C Name=$name")  
  4.         println("A/C Number=$number")  
  5.     }  
  6. }  
  7. class PersonalInfo:AccountInfo("Mani",101005){  
  8.     var email:String="[email protected]"  
  9.     var contact:Long=8882265032L  
  10.     fun showContactInfo(){  
  11.         println("Email=$email")  
  12.         println("Contact=$contact")  
  13.     }  
  14. }  
  15. fun main(args:Array<String>){  
  16. var pi=PersonalInfo()  
  17.     pi.publicInfo()  
  18.     pi.showContactInfo()  
  19. }  
  20. /*output:A/C Name=Mani 
  21. A/C Number=101005 
  22. [email protected] 
  23. Contact=8882265032*/  
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.

kotlin
Example

  1. open class Employee(var empid:Int){  
  2.     fun showId(){  
  3.         println("Employee Id="+empid)  
  4.     }  
  5. }  
  6. open class AddressInfo(var address:String,var id:Int):Employee(id){  
  7.     fun showAddress(){  
  8.         println("Address=$address")  
  9.     }  
  10. }  
  11. open class ContactsInfo(var number:Long,var add:String,var userid:Int):AddressInfo(add,userid){  
  12.     fun showContacts(){  
  13.         println("Contact=$number")  
  14.     }  
  15. }  
  16. fun main(args:Array<String>){  
  17. var info=ContactsInfo(8882265032,"Delhi",5001)  
  18.     info.showId()  
  19.     info.showAddress()  
  20.     info.showContacts()  
  21. }  
  22. /*output: 
  23. Employee Id=5001 
  24. Address=Delhi 
  25. Contact=8882265032 
  26. */                                                                    

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.

kotlin

Example

Let us understand this with an example .

kotlin

  1. interface Cars {  
  2.     fun carsspeed()  
  3. }  
  4. interface Bikes {  
  5.     fun bikesspeed()  
  6. }  
  7. class Vichel : Cars,Bikes{  
  8.     override fun carsspeed() {  
  9.         println("Car is running on 100 km/hrs")  
  10.     }  
  11.   
  12.     override fun bikesspeed() {  
  13.         println("Bike is running on 60 km/hrs")  
  14.     }  
  15. }  
  16. fun main(args:Array<String>){  
  17.     var vichel=Vichel()  
  18.     vichel.bikesspeed()  
  19.     vichel.carsspeed()  
  20. }  
  21. /* 
  22. Bike is running on 60 km/hrs 
  23. Car is running on 100 km/hrs 
  24. */  

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.

Conclusion

This 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.


Similar Articles