Linearization
As we already saw, traits offer a form of multiple inheritance. In such cases, the hierarchy is not necessarily linear, but forms an acyclic graph that needs to be flattened upon compilation. What linearization does is this: it specifies a single linear order for all of the ancestors of a class, including both the regular superclass chain and the parent chains of all of the traits.
We will not have to deal with linearization in traits that contain no code. However, if we use mixins, we will have to consider it. The following will be affected by linearization:
- Method definitions
- Variables (both mutable—
var
, and immutable—val
)
We already saw a simple example of linearization previously. Things, however, can get much more complicated and unexpected if the rules of linearization are not clear.
Rules of inheritance hierarchies
Before looking into linearization rules, we need to be clear on some inheritance rules in Scala:
- In Java, even if a class does not explicitly extend...