Defining an interface property

Kotlin allows us to define interface properties as well. An interface property can include a value assignment or can include just the property definition, in which case an implementing class is required to either provide a value or to be marked abstract. In the following example, we've added an id property to the GameObject interface:

interface GameObject {
val id: String
fun update(currentTime: Long)
}

Our Player class is now required to either be marked abstract or to override the id property. In this case, id is overridden using a single expression: 

class Player : GameObject {

override val id = "playerId"

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

It's also possible to override an interface property using a custom accessor, as in the following code:

class Player : GameObject {

override val id: String
get() =
"playerId"

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

It's possible to provide a default value for the interface property. In such a case, it's no longer required to override the property in any implementing class. In the following snippet, we've added a default value for the id property:

interface GameObject {
val id: String
get() =
"defaultId"
fun update(currentTime: Long)
}

After adding a default value for id, we no longer need to override it in the implementing Player class:

class Player : GameObject {
// no longer required to override id

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

In the next section, we'll see that interfaces may inherit from other interfaces to compose a functionality.