- Kotlin supports inheritance, which allows you to define a new class based on an existing class.
- The existing class is known as the superclass or base class, and the new class is known as the subclass or derived class.
- The subclass inherits all the properties and functions of the superclass, and can also add new properties and functions or override the properties and functions inherited from the superclass.
- Inheritance is one of the more important features in object-oriented programming.
- Inheritance enables code re-usability, it allows all the features from an existing class(base class) to be inherited by a new class(derived class). In addition, the derived class can also add some features of its own.
Advantages of using inheritance in Kotlin:
- Code Reusability: Inheritance allows you to reuse code from existing classes, reducing the amount of code you have to write and maintain.
- Improved Abstraction: By creating a class hierarchy, you can create an abstraction layer that makes your code more maintainable and less prone to bugs.
- Polymorphism: Inheritance allows you to create objects of different types that have the same interface, which is a fundamental aspect of object-oriented programming and enables polymorphic behavior.
Disadvantages of using inheritance in Kotlin:
- Complexity: Inheritance can make your code more complex, especially if you have many classes in a deep class hierarchy.
- Coupling: Inheritance creates a tight coupling between the superclass and the subclass, which can make it harder to change or modify the superclass without affecting the subclass.
Syntax of inheritance:
open class baseClass (x:Int ) { .......... } class derivedClass(x:Int) : baseClass(x) { ........... }
→ In Kotlin, all classes are final by default. To permit the derived class to inherit from the base class,
we must use the open keyword in front of the base class.
Kotlin Inheriting property and methods from base class:
When we inherit a class then all the properties and functions are also inherited. We can use the base class variables and functions in the derived class and can also call functions using the derived class object.
//base class open class baseClass{ val name = "GeeksforGeeks" fun A(){ println("Base Class") } } //derived class class derivedClass: baseClass() { fun B() { println(name) //inherit name property println("Derived class") } } fun main(args: Array<String>) { val derived = derivedClass() derived.A() // inheriting the base class function derived.B() // calling derived class function }
Kotlin inheritance primary constructor:
If the derived class contains a primary constructor, then we need to initialize the base class constructor using the parameters of the derived class. In the below program, we have two parameters in primary constructor of base class and three parameters in derived class.
//base class open class Employee(name: String,age: Int) { init{ println("Name of the Employee is $name") println("Age of the Employee is $age") } } // derived class class CEO( name: String, age: Int, salary: Double): Employee(name,age) { init { println("Salary per annum is $salary crore rupees") } } fun main(args: Array<String>) { CEO("Sunder Pichai", 42, 450.00) }
Overriding Member functions and properties:
- If the base class and derived class contain a member function with the same name, then we can override the base member function in the derived class using the override keyword and also need to mark the member function of the base class with open keyword.
- Similarly, we can override the property of the base class in the derived class.
Kotlin program of overriding the member function :
// base class open class Animal { open var name: String = "Dog" open var speed = "40 km/hr" open fun run() { println("Animals can run") } } // derived class class Tiger: Animal() { override var name = "Tiger" override var speed = "100 km/hr" override fun run() { // overrides the run method of base class println("Tiger can run very fast") } } fun main(args: Array<String>) { val t = Tiger() t.run() }
Calling the super class implementation:
We can also call the base class member functions or properties from the derived class using the super keyword. In the below program we have called the base class property color and function displayCompany() in the derived class using the super keyword.
// base class open class Phone() { var color = "Rose Gold" fun displayCompany(name:String) { println("Company is: $name") } } // derived class class iphone: Phone() { fun displayColor(){ // calling the base class property color println("Color is: "+super.color) // calling the base class member function super.displayCompany("Apple") } } fun main(args: Array<String>) { val p = iphone() p.displayColor() }