Overriding rules
You decided your new class has to redefine one of the methods inherited from one of the parent classes. This is known as overriding; I have already used it in the previous chapter. If you have already programmed in Java, you will find Kotlin a more explicit language. In Java, every method is virtual by default; therefore, each method can be overridden by any derived class. In Kotlin, you would have to tag the function as being opened to redefine it. To do so, you need to add the open
keyword as a prefix to the method definition, and when you redefine the method, you specifically have to mark it using the override
keyword:
abstract class SingleEngineAirplane protected constructor() { abstract fun fly() } class CesnaAirplane : SingleEngineAirplane() { override fun fly() { println("Flying a cesna") } }
You can always disallow any derived classes from overriding the function by adding...