Introduction
Object-oriented programming languages rely heavily on Inheritance. Inheritance allows you to pass a feature from an existing class (or base or parent class) to a new class (or derived class or child class).
In this article, we will learn about Kotlin Inheritance and the need to use them. But before jumping into this, we would strongly recommend learning about Kotlin Functions.
Now let us study Implementing Inheritance in Kotlin.
Kotlin Open Class
The primary class is the superclass (or parent class), and the class that inherits from the superclass is the subclass (or child class). The subclass incorporates characteristics from the superclass as well as its own.
When two or more classes have identical attributes, the concept of Inheritance is allowed. It enables code reuse. A derived class has a single base class but many interfaces, whereas a base class can have one or more derived classes.
The derived class in Kotlin inherits a base class via the: operator in the class header (after the derived class name or constructor)
Syntax:
open class BaseClass(no: Int){
}
class DerivedClass(no: Int) : BaseClass(no){
}
Assume we have two distinct classes, Circle and Rectangle, each with the specific property and side and their particular function property(). We can inherit the Shape class with similar characteristics thanks to the inheritance feature.
Code:
open class Shape(private val side: Int) {
protected open fun property(){
println("this shape has $side sides")
}
}
class Circle(private val property: String, private val side: Int): Shape(side) {
public override fun property(){
super.property()
println("this $property has $side sides")
}
}
class Rectangle(property: String, side: Int): Shape(side) {
// private override fun property(){ // override property incompatible with private
// println("this $property has $side sides")
// }
}
fun main() {
val circle = Circle("circle with radius 10",0)
circle.property()
}
Output:
this shape has 0 sides
this circle with radius 10 has 0 sides
We are making a Circle class object in our main(). As we know, CircleCircle is inheriting the properties of the Shape class; hence, we can override the property() of the Shape class in our Circle Class.
super.property() will call the parent property(), which will have side value as same as that of Circle side value. As we have defined that CircleCircle will have two variables while declaring. One is property as a String value, and the second is side as an Integer value. We will be passing this Integer Value to the Shape class and assigning the value to the Shape class side variable.
If we want to use property() of the derived class only then, we don't have to call super.property() to hit up the parent property() method.
For example, the class Any is implicitly inherited by the class Demo.
class Demo
Because Kotlin classes are final by default, they cannot be easily inherited. To inherit a class and make it non-final, we use the open keyword before the class.
open class Demo{
// Demo could be extended!
}
We are using the override keyword to use the parent class method in its subclass.
Kotlin Override Keyword
In other words, method overriding occurs when a subclass redefines or alters the method of its superclass. Only Inheritance allows for method override.
Rules of Method Overriding
- The parent class and the method or property overridden must be open (non-final).
- The method names of the base and derived classes must be the same.
- The method must have the same parameters as the underlying class.
Kotlin Superclass Implementation
Using the super keyword, a derived class can invoke the methods and properties of its superclass.
Using the super keyword, a derived class can also invoke the methods and properties of its superclass.
The superclass Any is shared by all Kotlin classes. It is the default superclass for a class with no explicitly declared supertypes.





