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

Get Programming Groovy 2 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.