Summary
There is a lot of interest currently in the ability to extend a class (or something close to class extension) without requiring recompilation or source changes, Traits offer a viable method of achieving this.

Advertisement

What are Traits?

Traits are like interfaces, but can have a method body and a class that implements a Trait inherits the method body. In many ways Traits are similar to abstract classes, but they don't have constructors, don't have initializers, don't have fields, can be multiply inherited (like an interface), and super does not refer to a method in an inherited trait, instead of super you have to qualify the intended name [TraitName].[methodName]( ... ).

By way of example consider current best practice: you write an interface, e.g. List, write a useful abstract class to make writing lists easier, AbstractList, and then implement some lists, e.g. ArrayList. This is all well and good, but suppose you now want to add some extra methods to List, e.g. sort. You can't because every class that implements List would need to add a sort method and you don't have control over all the source that uses List. So instead you use a helper class, Collections, and add sort as a static method. The user then calls sort( list ) (assuming a static import is used). A better technique is a Trait, with a trait you modify List:

Note on Mixins

The difference between a Trait and a Mixin is that order is important. In the class XY example given above, with a Trait you have to explicitly say which m you want. With a Mixin the order in which the interfaces are mixed in determines what happens. class XY implements X, Y {} is read as first mixing in X then mixing in Y; hence the m from Y overrides the m from X, since it is mixed in afterwards.

Note on Extension Methods

An alternative way of adding methods to a class is Extension Methods, these are proposed for Java 7, but have a number of problems.

Extending existing classes

To be able to add methods to an interface without recompiling, all clients of the interface, the class loader has to be extended to automatically add in the extra methods if they are not present and to flag an error if an unresolved conflict exists. Currently the class loader flags a missing method, the required extra action would be to see if the missing method is in an implemented interface and if it can be added without conflict.

As an alternative to abstract classes

Traits would be a viable alternative to abstract classes in many cases, for example the methods in AbstractCollection could be moved to Collection and no one would need to use AbstractCollection in the future. But a Trait cannot have any fields, therefore some abstract classes, e.g. AbstractList (it contains a field) would still be needed, although some methods may be moved to an interface.

Conclusions

I think Traits would be a useful addition to Java, what do others think?

Of course, all code that implements MySpecialList that doesn't extend AbstractList will break. The counter arguments are:

1) Just how many cases of this are there?2) Those guys can implement sort() with one line of code.3) If all else fails, those guys can stick to Java 6.

Obviously, I've picked a very simple example, and "traits" would often be used in more complex cases. But the need to resolve multiple inheritance-like conflicts adds complication, maybe it's not worth it?

Traits (or simply implementations in interfaces) had been already rejected in the JCP. There is little chance that they will make it into the language.

It is though possible to mimick traits, even in an open class manner, with Java. I wrote Open-Traits http://code.google.com/p/open-traits/ some weeks ago - it is not finished but demonstrates well the use and the caveats of this technique

The main difference between the my proposals is the language extension would enable compile time checks whereas your proposal would be runtime. To some extent your FixedTrait addresses this balance. There would still be some limitations. EG1 class with constructor arguments:

I am well aware that other languages, particularly dynamic languages, have a Traits or similar since Lisp days. The twist I am adding is allowing the loader to extend classes, thereby giving some dynamic language flexibility to a statically typed language.

Thanks for saying you like the Traits suggestion. Some things have got up in Java after initial rejection - so you never know. Personally I think it is unlikely that anything will get up that isn't backed by a big company. But it is good fun interacting with people who have great ideas.

Hi, that seems be problem.xy=XY();now take xy as X, I hope call X(xy).m();but after a smoking, I hope call Y(xy).m();According to X Y interface protocol, I should get 2 m();The above declaration seems not very clear?I seem catch some thing from memory, eiffel get a not bad solution for MI. maybe I should check it again.