O'Reilly logo

Scala Functional Programming Patterns by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required