I tried to access variable STR_PARENT from ParentClass.kt in Activity function like ChildClass.STR_PARENT, but it doesn't allow. How to achieve it.
open class ParentClass {
companion object {
const val STR_PARENT = "str_parent"
}
}
class ChildClass : ParentClass() {
companion object {
const val STR_CHILD = "str_child"
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ChildClass.STR_CHILD // works
ParentClass.STR_PARENT // works
ChildClass.STR_PARENT // doesn't work
}
}
As you can see, in onCreate function I can access variable like : ParentClass.STR_PARENT, but I can't access same variable like : ChildClass.STR_PARENT in Kotlin. Same thing is possible in Java but in Kotlin it is not working.
I didn't find anything regarding this in Kotlin inheritance doc too.