Difference between revisions of "Kotlin"
From zen2
| Line 1: | Line 1: | ||
==Class== | ==Class== | ||
| + | ===Open=== | ||
By default Kotlin classes are final, meaning no classes can inherit from them. In order to make them not final declare them open | By default Kotlin classes are final, meaning no classes can inherit from them. In order to make them not final declare them open | ||
<pre> | <pre> | ||
open class Vehicle() {} | open class Vehicle() {} | ||
</pre> | </pre> | ||
| + | |||
| + | ===Override=== | ||
To override class functions they must also be declared open | To override class functions they must also be declared open | ||
<pre> | <pre> | ||
| Line 12: | Line 15: | ||
class Car() : Vehicle() { | class Car() : Vehicle() { | ||
override fun drive() { | override fun drive() { | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ===Super=== | ||
| + | To use a function from the parent class use super | ||
| + | <pre> | ||
| + | open class Vehicle() { | ||
| + | open fun drive() { | ||
| + | println("original") | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Car() : Vehicle() { | ||
| + | override fun drive() { | ||
| + | println("some stuff") | ||
| + | super.drive() //will append "original" | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||
Revision as of 23:17, 18 November 2017
Contents
Class
Open
By default Kotlin classes are final, meaning no classes can inherit from them. In order to make them not final declare them open
open class Vehicle() {}
Override
To override class functions they must also be declared open
open class Vehicle() {
open fun drive() {}
}
class Car() : Vehicle() {
override fun drive() {
}
}
Super
To use a function from the parent class use super
open class Vehicle() {
open fun drive() {
println("original")
}
}
class Car() : Vehicle() {
override fun drive() {
println("some stuff")
super.drive() //will append "original"
}
}
