Inheritance

In Kotlin, all classes inherit from the class Any. Any is the default superclass if we don’t specify a superclass.

If we want to make a class a superclass we have to use the keyword open to allow that other classes can inherit it.

To make a class inherit from another we just have to put “:” and the name of the parent class:

If the child class has a primary constructor, the parent class must be initialized using the parameters of the primary constructor.

If the child class doesn’t have a primary constructor, the secondary constructor has to initialize the parent with the super keyword:

Overriding Methods:

To override functions in Kotlin we have to do 2 things:

1. Use the keyword open in the parent class for the functions that you want to make overridable.

2. Use the keyword override in the child class which override the function:

You can override properties in the same way than the functions, using the keywords open and override.