Is it possible to override Java getter (method) using Kotlin val (property)?

For instance:

Java:

public class Foo {

    public int getSomething() {
        return 1;
    }

}

Kotlin:

class Bar : Foo() {

    // works
    override fun getSomething() = 2

    // doesn't work ('something' overrides nothing)
    // override val something = 2

}

I thought it val something = 2would be converted to public int getSomething() { return 2; }in bytecode Java.

+4
source share
1 answer

This seems to be a known issue here . Apparently, this is a difficult issue and is unlikely to be resolved in the near future.

Original answer to a question from Andrei Breslav:

This is a pretty deep problem, unfortunately. It is unlikely that we will ever work the way you would like

Further on the problem page, you can see that it has become even more complex in relation to multi-platform projects.

+2
source

Source: https://habr.com/ru/post/1689733/


All Articles