Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

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...