What is inheritance?

Inheritance is one of the key concepts in object-oriented programming. It involves avoiding code-repetition, especially where different classes have common features and all the classes belong to the same type. Let's say we are writing software for an educational institute in which three different entities are involved—Student, Professor, and Employee:

All these entities have some properties in common, including name, age, and id. Each entity exhibits some behaviors, such as speak or greet. Let's convert the diagram of the Professor class into code:

class Professor(val fName: String, var lName: String, var pAge: Int, val professorId : String ) {

fun speak() {
println("My name is $fName $lName age is $pAge and my
ID is $professorId")
}

fun greet() {
println("Hi there... Professor $fName ")
}

fun aboutSpecialization() {
println("I have a done my Phd in Computer science")
}

fun deliverLecture() {
println("I am teaching Introduction to Kotlin.")
}
}

fun main(args: Array<String>) {
val jon = Professor("Jon","Jack",40, "PR-101")

jon.greet()
jon.speak()
jon.aboutSpecialization()
jon.deliverLecture()
}

This example works fine, but it is a poor way to solve our problem. If we look closely, we can see that each class contains similar lines of code because of their similar attributes and behaviors. If we decide to change how a particular function works, or if we add a new attribute, such as address, in each entity, we would need to implement new requirements for all three classes. The preceding implementation may be acceptable up until this point because of the low complexity of the program. As new requests are received, however, for new classes, such as graduate student, visitor, or contractor, the maintenance of the code will become a nightmare.  

Inheritance is a technique that helps us remove code-duplication. Inheritance identifies all the common features of different classes and combines them into one class. All the classes with the same features can be inherited from a parent class. A student, an employee, and a professor all belong to a person class and each person has a name, an age, and an ID. Let's create a class called Person and combine all the common attributes and behaviors in it:

Once the Person class has been created, all other classes can extend this class. In inheritance, the main class is called a super or parent class. Classes that extend this class are called child classes. All child classes automatically include the functionality of the parent class. In Kotlin, the inheritance syntax is as follows:

class Child  :  Parent {
… …
}

Let's convert the diagram of the Person class into code:

class Person (val fName: String, var lName: String, var pAge: Int, val id : String) {

fun speak() {
println("My name is $fName $lName, my id is $ID and age is $pAge")
}

fun greet() {
println("Hi How are you...")
}
}

Each child class can inherit the Person class and utilize all the properties and behaviors of the parent class. It can also have its own behaviors. As we can see, the Person class contains three important properties: first name, last name, and age. These are required for every person. When the child class inherits the parent class, it is necessary to pass the required variables to the parent class. We can now create a Student class and inherit the Person class as follows:

class Student(fName: String, lName: String, pAge: Int, ID: String ) : 
Person(fName, lName, pAge, ID) {
}

As soon as we inherit the Parent class, the compiler throws an error that states this class is final and cannot be inherited from. This is because each class is final by default and we must open them before extending them. A complete example of this code can be found on GitHub: https://github.com/PacktPublishing/Hands-On-Object-Oriented-Programming-with-Kotlin/tree/master/src/main/kotlin/Chapter03.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.154