17.2 Subclassing Syntax
As a safety measure designed to make Kotlin code less prone to error, before a subclass can be derived from a parent class, the parent class must be declared as open. This is achieved by placing the open keyword within the class header:
open class MyParentClass {
var myProperty: Int = 0
}
With a simple class of this type, the subclass can be created as follows:
class MySubClass : MyParentClass() {
}
For classes containing primary or secondary constructors, the rules for creating a subclass are slightly more complicated. Consider the following parent class which contains a primary constructor:
open class MyParentClass(var myProperty: Int) {
}
In order to create a subclass of this class, the subclass declaration references any base class parameters while...