Inheritance is the concept of expanding a class using another class. By doing so, the class that is being expanded will inherit all the properties and functions from the class it used for the expansion.
Inheritance is the concept of expanding a class using another class. By doing so, the class that is being expanded will inherit all the properties and functions from the class it used for the expansion.
Syntax
class OriginalClass() : [inherit_class] () { }
Example
In the example, the SoccerBall class inherited from the Ball class.
The open Keyword
By default, Kotlin classes are final so they cannot be inherited. To make a class inheritable, declare it with the open keyword:
Overriding
The term override means to overwrite the logic of an existing function that existed in the class it inherited.
It is the same for properties - variables.
Super Class
The term Super Class refers to the parent class that the class inherited. For the SoccerBall class, the super class is the Ball class.
We can access the properties and function from the super class by using the super keyword.
super.roll()
One of the reason you may want to do this is to expand the current definition of a function.
In the example, the SoccerBall class overridden the roll() function so when we create a Soccer Ball object and call the roll() function, it will print "Soccer ball is rolling..."
What if we want the roll function to also print the original text from the Ball class? We can call the roll() function from the super class in the overridden class to maintain it.