Defining a simple interface

Defining an interface in Kotlin is quite similar to Java. To declare a new interface, you must add the interface keyword followed by the interface name as in the following code:

interface GameObject

It's not required to provide an interface body or any methods or properties. An empty interface might be used as a simple marker interface.

We can add an interface method by defining a method signature to be required by the interface:

interface GameObject {
fun update(currentTime: Long)
}

A class or object can them implement this interface method. To do this, the class must include interface in its declaration and then either implement the required methods or be marked abstract. The following snippet demonstrates a Player class, which implements our GameObject interface:

class Player : GameObject {
override fun update(currentTime: Long) {
// add logic
}
}

In Kotlin, the override modifier is required by the compiler to indicate that an interface or class method is being overridden. Without the override keyword, you will get a compiler error because the method will hide a member from the supertype interface.