Overriding members
Reimplementing functions or properties from a base type is called overriding. Overriding is also a way to achieve polymorphism, another aspect of object-oriented programming. In Kotlin, you can override them only if the base type allows it. By default, all members in Kotlin are final, that is, cannot be overridden. To allow a subclass to override a member, it has to have the open
keyword. This is in contrast to Java, where by default every member is overridable. Another difference from Java is that whenever you are overriding a member, you have to have the override
keyword present. Java doesn't have such a keyword and overriding is considered with just declaring a function with the same name and signatures as the base type. This can be error-prone and that's why Java uses the @Override
annotation to make the developer's intentions clearer:
open class Base { open fun print() { println("I'm called from the base type") } } class Derived : Base() { override fun print(...