Kotlin – Override Method

To override method of a Super class, define a function in the Child class with same definition as that of in Super class. Overriding a method of Super class is useful, when you need to change the default behaviour. Also note that only those functions that are open in Super class could be overridden.

Syntax – Kotlin Inheritance

Following is the syntax to override method of a Parent class.

Note : The method to be overridden has to be open.

Example – Kotlin Override method of super class

Following example demonstrates Inheritance in Kotlin. We shall take the above example scenario of Student, Teacher : Person.

When the above program is run

Conclusion :

In this Kotlin TutorialOverride Method of Super Class, we have learnt that the function that could be overridden has to be open, and the overriding function definition in Sub class has to be same as that of in Super class. We also saw a Kotlin Example to override method of super class.