The five printed lines, including the comma and space after every fragment, are:Scala dynamic dispatch uses the receiver's runtime software class, even though
f1 in C,
f1 in C, f2 in B,
f1 in C, f2 in A, f3 in B,
f1 in A, f2 in A, f3 in ObjB, f4 in B,
f1 in ObjC, f2 in A, f3 in B, f4 in ObjC, f5 in C, a has the static type A. Thus a.f1 uses C.f1, and a.f2 uses B.f2 whose unqualified f1 still dispatches on the new C receiver.For
a.f3, B.f3 explicitly selects super.f2, which is A.f2. Inside A.f2, the unqualified call to f1 still dispatches to C.f1; super does not replace the receiver. For a.f4, B.f3 in the body of class B means a call on the singleton object B, not on this. That object's f3 calls A.f2, which calls A.f1.Finally
a.f5 calls C.f4 on the singleton object C. That object extends class B, overrides f1 and f4, and inherits class B's f3. Its f4 calls this inherited f3; that selects A.f2, whose f1 dispatches to the object's own override. These distinctions follow member selection and super semantics.[ref]Scala 2.11 expressions specification. This and Super; Designators, member selection
List each Scala trait linearization from the most specific software class toward the common base, omitting predefined types:For example,
Life1, Party, Study, Life
Life2, Sport, Exercise, Research, Party, Study, Life
Life3, Research, Study, Sport, Party, Exercise, LifeSport alone linearizes as Sport, Party, Exercise, Life, while Research alone gives Research, Party, Study, Life. In combining parents, take the right parent's linearization first, deleting from it entries also present in the remaining left-parent linearization, then append the latter. This places a common base after all of its derived traits.[ref]An unqualified The first three lines belong to
super.describe() continues after the current trait in the actual receiver's Scala trait linearization. Each override prints after its super call, so the output follows the displayed order backwards, starting at Life. The complete output is:I live.
More on study.
More on party.
I live.
More on study.
More on party.
More on research.
More on exercise.
More on sport.
I live.
More on exercise.
More on party.
More on sport.
More on study.
More on research.l1, the next six to l2, and the final six to l3. Articles by others on the same topic
There are currently no matching articles.