Over-riding rules in Kotlin

First, we have an abstract class. Remember that our abstract functions/properties MUST be over-ridden.

In order to use this abstract class, we can use an open class. An open class means it can be extended. It can also extend from abstract class Course. When we extend Course, we initialize its properties. We’re not happy with its default learn function, so we override it and implement it ourselves.

open in abstract means it is over-ridable. If there is no open keyword, then the default keyword ‘final’ applies.

As you can see, when we instantiate KotlinCourse, we execute learn function and see that it calls KotlinCourse’ learn function.

What we have a specialized class SpecializedKotlinClass that extends KotlineCourse?

By default, we can override the learn function in KotlinCourse. If we DO NOT override it, it will simply call KotlinCourse’s learn function.

However, say we don’t want people to over-ride KotlinCourse’s function. Any class that extends KotlinCourse MUST USE its learn function.

In order to prevent others to -override KotlinCourse’ learn function, simply put final override fun.
Thus, this prevents SpecializedKotlinClass from overriding the learn function. Doing so would give you a compiler error.

Multiple Super Types

Say we create another interface with a learn function:

And we extend both from abstract and interface…where both have function learn. When you use super, the compiler will be confused which super type you are referring to. In this case, we can use the bracket and the super type name to designate whether you want to use the interface or the abstract one.