Implementing an Interface
To use an interface, we say that you “implement” it on a class. There are two parts to this: First, you declare that the class implements the interface. Then, you must ensure that the class provides implementations for all of the properties and functions specified in the interface.
Use the :
operator to implement the Fightable interface on Player.
Listing 16.2 Implementing an interface (Player.kt
)
class Player(_name: String, override var healthPoints: Int = 100, var isBlessed: Boolean = false, private var isImmortal: Boolean) : Fightable { ... }
When you add the Fightable interface to Player, IntelliJ indicates that functions and properties are missing. Warning you that properties and functions ...
Get Kotlin Programming: The Big Nerd Ranch Guide, First Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.