13.4 Injecting Methods Using Mixins
In Java we can inherit from multiple interfaces, but we’re allowed to extend from only one class. Groovy carries the Java semantics, but in addition offers the flexibility to pull in implementations from multiple classes.
The issues surrounding multiple interfaces in languages like C++ prompted the restrictions in Java. When multiple implementations are pulled together, the implementations can collide and cause a great deal of confusion. Groovy avoids those issues by allowing methods to compose and collaborate rather than collide, as we’ll see in this section.
Groovy mixins are a runtime capability that we can use to bring in or mix in implementations from multiple classes. If we mix a class into another, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access