0
votes

I want to be able to call an abstract method of the super class like

trait B { def x: Int }
trait D extends B { def y = super.x }

However, this doesn't work. Even the abstract override modifier doesn't help.

Is it possible to do this in scala? If no, what is the reason?

I know that this works when I override method x in trait D, but is it possible from method y?

2
Simply call x from D. - Till Rohrmann

2 Answers

1
votes

If you have a definition like

trait B { def x: Int }

then the method x is not implemented and that is the reason why it does not work.

trait D extends B { def y = super.x }

You try to call something here that is not implemented, but if you want to refer to specific superclass you can do like this:

    trait C { def x : Int = 42 }
    trait D extends C { def y : Int = super[C].x }

C now specifies the superclass that is the call target, if you specify nothing, the call is dispatched by the order of the linearization of D.

1
votes

You can't call unimplemented method, but in this case you can call x without super:

trait B { def x: Int }
trait D extends B { def y = x }

For understanding the reason you can read about linearization in scala.