You can’t access bar inside chad1(), because bar is a constructor parameter and is available only during the initialization of the object. You have to make it a val if you want to use it anywhere outside the constructor.
Are you just trying to reference something from super?
open class Base {
val bar: String
constructor(bar: String) {
this.bar = bar
}
}
class SubType : Base {
val myBar: String
val superBar: String
get() = super.bar
constructor(bar: String) : super("Super $bar") {
myBar = bar
}
}
fun main() {
val foo = SubType("bar")
println(foo.bar)
println(foo.myBar)
println(foo.superBar)
}
I’d recommend going through something like Atomic Kotlin–it seems like it would answer a lot of your questions better than I can .
Alternatively, maybe you’re trying to keep a reference to the constructor param bar? You can just save it under another name in the constructor. I typed out the constructors to help make them more obvious instead of leaving them inline with the class declaration. Notice how SubType keeps its own reference to the constructor param under a new name myBar.