The dreaded diamond
Mules are hybrid animals. Charles Darwin found them most surprising. Mules possess more reason, memory, obstinacy, social affection, powers of muscular endurance, endurance, and length of life than either of their parents, namely donkey and horse.
Now, the question is how would we model mules in our system? Mules obviously walk and move goods. So, we might be tempted to model mules by extending both Horse
and Donkey
. Alas! We cannot! Java allows a class to extend from only one class—also known as a single inheritance. We don't wish to rewrite the walk
and moveGoods
methods again for mules. If the language allowed us to extend mules
from both Horse
and Donkey
, it would be just the thing! Let's see the following diagrammatic representation for this example:
The problem here is the walk() method. As the diagram shows, which walk method implementation would Mule inherit? Would it be the one from Horse? Or the one from Donkey?
You would think we...