October 2019
Intermediate to advanced
434 pages
11h 54m
English
Like in Java 8, Kotlin interfaces provide a default implementation for methods. To add a default implementation, we can define a function body within the interface declaration:
interface GameObject { val id: String get() = "defaultId" fun update(currentTime: Long){ println("id: $id update:") }}
In this example, we've added a default implementation for the GameObject.update method. If an interface provides a default implementation, it does not need to be overridden in an implementing class:
class Player : Movable { override fun move(currentTime: Long) { // add logic }}
You'll see in this example, that Player implements Movable, but does not need to override update:
class Player : Movable { override fun move(currentTime:
Read now
Unlock full access