Allowing overriding of interface properties using functions

Hey,
I find it kind of weird that defining a property in an interface forces the implementing class to implement it also as a property.

What I expected was that I would be able to choose whether to implement a property by an actual property or by defining a corresponding getter function (and setter if its a var):

interface Demo {
    val str: String
}

class DemoImpl : Demo {
    fun getStr() = "Hello"
}

IMO, an interface defines a contract and should know as little as possible about how this contract is implemented, therefore allowing to implement interface properties using getter and setter functions feels only natural.

What do you guys think?

How is that DemoImpl preferable to

class DemoImpl : Demo {
    override val str get() = "Hello"
}

?

1 Like