Thursday 4 January 2018

Kotlin: Inheritance

Inheritance is the concept of re usability. Object of one class can get the properties and methods of object of another class by using inheritance.

By using inheritance, you can create new class by extending the existing class. The new class is called derived class and the existing class is called base class.



Example
open class BaseClass{

}

class DerivedClass : BaseClass{

}


By default, all the classes in kotlin are final. To make these class inheritable, you should use the keyword ‘open’.


Let’s see inheritance feature by an example. In every organization, there are two kinds of employees.
a.   Permanent Employees
b.   Contract Employees

Both the permanent and contract employees share properties like firstName, lastName and id. Where as contract employees has separate payroll company. We can place these common properties (firstName, lastName and id) in separate class Employee and make the classes PermanentEmployee and ContractEmployee inherit these properties from the common base class Employee.

open class Employee {

}

class PermanentEmployee : Employee {

}

class ContractEmployee : Employee {

}

Find the below working application.


Inheritance.kt
open class Employee {
 var firstName: String = ""
 var lastName: String = ""
 var id: String = ""

 constructor(firstName: String, lastName: String, id: String) {
  this.firstName = firstName
  this.lastName = lastName
  this.id = id
 }

 open fun getEmployee(): String {
  return "firstName : $firstName, lastName: $lastName, id: $id"
 }

}

class PermanentEmployee : Employee {
 var healthInsuranceNumber = ""
 var salary: Double = 0.0

 constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id) {
  this.healthInsuranceNumber = healtInsuranceNumber
  this.salary = salary
 }

 override fun getEmployee(): String {
  return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
 }

}

class ContractEmployee : Employee {
 var payRollCompany = "ABC"

 constructor(firstName: String, lastName: String, id: String, payRollCompany: String) : super(firstName, lastName, id) {
  this.payRollCompany = payRollCompany
 }

 override fun getEmployee(): String {
  return super.getEmployee() + ", payRollCompany: $payRollCompany"
 }
}


fun main(args: Array<String>) {
 var emp = PermanentEmployee("krishna", "Gurram", "123", "health3425", 56789.01)

 println(emp.getEmployee())
}


Output
firstName : krishna, lastName: Gurram, id: 123, healthInsuranceNumber: health3425, salary: 56789.01

constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id)
If super class don’t have default constructor, then all the sub class constructors must call the super class constructor explicitly.

By default, all the functions and classes are not inheritable (final), to make them inheritable, you should use the keyword ‘open’.

open class Employee {

         open fun getEmployee(): String {
                 return "firstName : $firstName, lastName: $lastName, id: $id"
         }

}

To override a function of super class, you should use override keyword.

class PermanentEmployee : Employee {

         override fun getEmployee(): String {
                 return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
         }

}

Important Points
a.   If the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.


b.   If the class has no primary constructor, then each secondary constructor has to initialize the base type using the super keyword, or to delegate to another constructor which does that. Note that in this case different secondary constructors can call different constructors of the base type:


Previous                                                 Next                                                 Home

No comments:

Post a Comment