I am practicing this code from JavaTpoint for learning inheritance in Scala. But I cannot access the member Bike from the class Vehicle who's value is initialized to zero. I tried by super type reference but it still shows the overridden value. Why does it not allow to access the super class field and directs to the overridden sub class field (speed) . here is the code and the output. Thanking in advance.
class Vehicle {
val speed = 0
println("In vehicle constructor " +speed)
def run() {
println(s"vehicle is running at $speed")
}
}
class Bike extends Vehicle {
override val speed = 100
override def run() {
super.run()
println(s"Bike is running at $speed km/hr")
}
}
object MainObject3 {
def main(args:Array[String]) {
var b = new Bike()
b.run()
var v = new Vehicle()
v.run()
var ve:Vehicle=new Bike()
println("SuperType reference" + ve.speed)
ve.run()
}
}
As we know, after Scala compile, Scala will be transfered to Java bytecode, it's for compatible with JVM.
And for the class
Vehicle
variableval speed
, after compile it's visible for it's subclassBike
(theprotected
variable), we can view thebytecode
ofVehicle
:As we can see, it's init the
speed
's value 10 in theVehicle
constructor method.and we also can find the init action in
Bike
constructor method:it's setting
100
forspeed
in constructor method.so when
init
theBike
object, thespeed
field's value has been updated to100
in thesuperclass
Vehicle
. sosuper.val
will not make sense in there.and there is another thing need to call out: when you use the
super.speed
directly in your subclassBike
, the compiler will throw:so this compiler error thrown is also caused by the above reason.
Answers to similar questions here overriding-vals-in-scala, or here cannot-use-super-when-overriding-values say:
Scala compiler does not allow to use super on a val
Why is that? Discussion in last link above points to: SI-899. And the first comment there says following:
it was changed so that traits could override vals to be more uniform with classes