Inheritance
As we already know, a supertype of all Kotlin types is Any
. It is the equivalent of the Java Object
type. Each Kotlin class explicitly or implicitly extends the Any
class. If we do not specify the parent class, Any
will be implicitly set as the parent of the class:
class Plant // Implicitly extends Any class Plant : Any // Explicitly extends Any
Kotlin, like Java, promotes single inheritance, so a class can have only one parent class, but it can implement multiple interfaces.
In contrast to Java, every class and every method in Kotlin is final by default. This conforms to the Item 17 in Effective Java, the Design and document for inheritance or else prohibit it rule. This is used to prevent unexpected behavior from a subclass changing. Modification of a base class can cause incorrect behavior of subclasses because the changed code of the base class no longer matches the assumptions in its subclasses.
This means that a class cannot be extended and a method cannot be overridden...