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 ...

Get Scala Functional Programming Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.