Running the code below:
class Parent {
val value = {
println("Setting value in parent")
"ParentVal"
}
println(s"Parent value is ${value}")
}
class Child extends Parent {
override val value = {
println("Setting value in child")
"ChildVal"
}
println(s"Child value is ${value}")
}
new Child
Produces this output:
Setting value in parent
Parent value is null
Setting value in child
Child value is ChildVal
So the code associated with the parent value assignment is run, however the value is not really assigned at the parent. Afterwards the child code runs and it assigns the value as expected.
Could someone explain the chain of events here at a lower level?