Tutorial by Examples

In Kotlin, classes are final by default which means they cannot be inherited from. To allow inheritance on a class, use the open keyword. open class Thing { // I can now be extended! } Note: abstract classes, sealed classes and interfaces will be open by default.
Defining the base class: open class BaseClass { val x = 10 } Defining the derived class: class DerivedClass: BaseClass() { fun foo() { println("x is equal to " + x) } } Using the subclass: fun main(args: Array<String>) { val derivedClass = Deri...
Defining the base class: open class Person { fun jump() { println("Jumping...") } } Defining the derived class: class Ninja: Person() { fun sneak() { println("Sneaking around...") } } The Ninja has access to all of the methods in Pe...
Overriding properties (both read-only and mutable): abstract class Car { abstract val name: String; open var speed: Int = 0; } class BrokenCar(override val name: String) : Car() { override var speed: Int get() = 0 set(value) { throw UnsupportedOpera...

Page 1 of 1