Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifier 'override' is not applicable to 'getter'

Tags:

kotlin

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

like image 834
ligi Avatar asked Oct 19 '25 11:10

ligi


1 Answers

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.

like image 200
yole Avatar answered Oct 21 '25 01:10

yole