Daily Archives: December 20, 2007

Mixins and Traits (for Java)

I’ve read last night a post on traits for Java and I just thought to make a small comment.

According to the author:

The difference between a Trait and a Mixin is that order is important.

Well, in my opinion there is a much bigger, fundemental difference: mixins have state, traits don’t. When a class or object includes a mixin it includes all methods and attributes defined by the mixin, according to the Wikipedia:

A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, not inherits, all the mixin’s attributes and methods.

This is quite an important difference, as traits are easier to be supported (except the inheritance change they are imposing), but the benefit is really limited. And the good news is that using AOP (e.g. AspectJ) you can have mixins (so implicitly you can have traits).

Posted by: Alex Popescu (aka the_mindstorm)

1 Comment

Filed under Uncategorized