An interface can inherit from another interface. In this case, we'll define a new interface, Movable, that inherits from GameObject:
- We'll start by declaring a new interface, Movable, which extends the GameObject interface from our previous examples:
interface Movable : GameObject
- We'll then add a new method to the new Movable interface:
interface Movable : GameObject {
fun move(currentTime: Long)
}
- We can then add new methods and properties to the new interface. Any class that then implements Movable will be required to implement the methods from both Movable and GameObject or else will be marked abstract:
class Player : Movable {
override fun update(currentTime: Long) {
// must implement this GameObject interface method
}
override fun move(currentTime: Long) {
// must implement this Movable interface method
}
}
Because the Player class implements Movable, it is required to either be abstract or implement the methods found on both Movable and GameObject since Movable extends GameObject. This type of interface inheritance provides an additional mechanism with which we can compose behaviors within our classes.