A mixin is a way of inheriting methods from a class without the problems associated with multiple inheritance. Groovy supports both compile time and runtime mixins.
Compile time mixins
Supported with the @Mixin annotation. First define the class that will hold the method definitions:
class Dog {
def talk() {
"Woof, woof"
}
}
Then mix in the method(s) to the target class:
@Mixin(Dog)
class Talk {
// other stuff here
}
Invoking talk.talk() will now give “Woof, woof” as the response.
Runtime mixins
You can also add methods to classes you don’t own by using the mixin() static method call:
someoneElsesClass.mixin(Dog)
For more information about mixins, see:
http://groovy.codehaus.org/Runtime+mixins