3
votes

I have something like this in the code

var barCode: BarCode? = null

now getBarCode() is defined in the interface - then I get the error that this might be an accidental override. unfortunately this does not work:

var barCode: BarCode? = null
override get

I could do something like this:

private var barCode: BarCode? = null

override fun getBarCode(): BarCode? = barCode
fun setBarCode(barCode: BarCode) {
    this.barCode = barCode
}

but this looks like way to many likes and verbosity for kotlin - there must be a shorter way - especially as this pattern will repeat multiple times in this class

1
And the interface is out of your control? You cannot convert the method in the interface to a property? - AndroidEx
the interface is java - ligi

1 Answers

5
votes

As of Kotlin 1.0, there is no shorter way: a method getBarCode() in a Java interface needs to be implemented by a method named getBarCode() in Kotlin, not by a property named barCode.

There's an issue requesting to make it possible to override Java methods with properties; you can vote for it to get notifications of updates.