Multiple inheritance

We inevitably had to mention multiple inheritance in the previous sections due to the fact that we can mix multiple traits and they can all have their own implementations of the methods. Multiple inheritance is not only a powerful technique, but also a dangerous one, and some languages such as Java have decided to not even allow it. As we already saw, Scala allows this, but with some limitations. In this subsection, we will present the problems of multiple inheritance and show how Scala deals with them.

The diamond problem

Multiple inheritance suffers from the diamond problem.

Let's have a look at the following diagram:

Here, both B and C extend A and then D extends B and C. Some ambiguities might arise from this. Let's say that there was a method that was originally defined in A, but both B and C override it. What would happen if D calls this method? Which one will it exactly call?

All the preceding questions make things ambiguous and this could lead to mistakes. Let's try and reproduce this in Scala using traits:

trait A { 
  def hello(): String = "Hello from A" 
} 

trait B extends A { 
  override def hello(): String = "Hello from B" 
} 

trait C extends A { 
  override def hello(): String = "Hello from C" 
} 

trait D extends B with C { 
  
} 

object Diamond extends D { 
  def main(args: Array[String]): Unit = { 
    System.out.println(hello()) 
  } 
}

What would be the output of program? Here is the output:

Hello from C

What if we just change the D trait to look like the following:

trait D extends C with B { 
  
}

Then the output of our program will be:

Hello from B

As you can see, even though the example is still ambiguous and prone to errors, we can actually tell which method will be exactly called. This is achieved using linearization, which we will look at in greater depth in the next section.

The limitations

Before focusing on linearization, let's point out the multiple inheritance limitations that Scala imposes. We already saw many of them before, so here we will simply summarize them.

Tip

Scala multiple inheritance limitations

Multiple inheritance in Scala is achieved using traits and it follows the rules of linearization.

In the inheritance hierarchy, if there is a trait that explicitly extends a class, the class that mixes in this trait must also be a subclass of the trait parent. This means that when mixing in traits that extend classes, they must all have the same parent.

It is not possible to mix traits in, which define or declare methods with the same signatures but different return types.

Special care has to be taken when multiple traits define methods with the same signatures and return types. In cases where the methods are declared and expected to be implemented, this is not an issue and only one implementation is enough.