Scala : Why can't we do super.val?

前端 未结 2 1099
孤街浪徒
孤街浪徒 2021-01-12 22:58

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.

相关标签:
2条回答
  • 2021-01-12 23:48

    As we know, after Scala compile, Scala will be transfered to Java bytecode, it's for compatible with JVM.

    And for the class Vehicle variable val speed, after compile it's visible for it's subclass Bike(the protected variable), we can view the bytecode of Vehicle:

      public Vehicle();
        Code:
           0: aload_0
           1: invokespecial #63                 // Method java/lang/Object."<init>":()V
           4: aload_0
           5: bipush        10
           7: putfield      #13                 // Field speed:I
          10: return
    

    As we can see, it's init the speed's value 10 in the Vehicle constructor method.

    and we also can find the init action in Bike constructor method:

      public Bike();
        Code:
           0: aload_0
           1: invokespecial #67                 // Method Vehicle."<init>":()V
           4: aload_0
           5: bipush        100
           7: putfield      #13                 // Field speed:I
          10: return
    

    it's setting 100 for speed in constructor method.

    so when init the Bike object, the speed field's value has been updated to 100 in the superclass Vehicle. so super.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 subclass Bike, the compiler will throw:

    super may not be used on value speed
    

    so this compiler error thrown is also caused by the above reason.

    0 讨论(0)
  • 2021-01-12 23:49

    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

    0 讨论(0)
提交回复
热议问题