Exactly like in Java, abstract classes are marked by abstract and interfaces by the interface keyword:
abstract class AbstractDungeonMaster { abstract val gameName: String
fun startGame() { println("Game $gameName has started!") } }
interface Dragon
As in Java 8, interfaces in Kotlin can have a default implementation of functions, as long as they don't rely on any state:
interface Greeter { fun sayHello() { println("Hello") } }
There are no inherits and implements keywords in Kotlin. Instead, both the name of an abstract class and all the names of the interfaces that class implements are put after a colon:
class DungeonMaster: Greeter, AbstractDungeonMaster() { override val gameName: String get() = "Dungeon of the Beholder" }
We can still distinguish the abstract class by the parenthesis that comes after its name, and there can still be only one abstract class, as there are no multiple inheritances in Kotlin.
Our DungeonMaster has access to both functions from Greeter and AbstractDungeonMaster:
val p = DungeonMaster() p.sayHello() // From Greeter interface p.startGame() // From AbstractDungeonMaster abstract class
Calling the preceding code, it will print the following output: