简体   繁体   中英

Not able to access protected member variable of Parent Kotlin class in Java child class

abstract class Parent {

    protected var name : String

}

class Child extends Parent {

    private void childMethod() {
         name = "child";
    }
}

in Child class it gives me error name has private access in xxxChild (xxx is package name)

In java child class we can access the protected member variable of parent as getName(). Like a getter method is auto-generated. I am new to Kotlin, need to dig out more how the method is auto-generated.

If you want to access it as a field from Java, you should annotate the Kotlin property with @JvmField . Note that per that link, you can only annotate a property with @JvmField if it has a backing field, is not private, does not have open , override or const modifiers, and is not a delegated property (none of which apply here, so you're fine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM