Aaron Sherman wrote:

Perhaps I'm slow, but I don't see the difference between a trait and a Java interface other than the fact that traits appear to be more of a run-time construct.

Java interfaces are actually a very nice compromise between multiple and
single inheritance.

You can not get rid of the method with Java interface. Plus, interface is only
declaration, you must implement methods in each class which use interface. AFAIK (last touched Java 5 years ago)


Look at example:
While the purpose of this paper is not to argue the merits of traits per se (see 
[SDNB03, BSD03] for such arguments), it is helpful to understand the motivation for 
traits. In languages with single inheritance, such as Smalltalk, it is often the case 
that inheritance does not provide sufficient flexibility for structuring a class 
hierarchy. Consider the case of two classes in different subtrees of the inheritance 
hierarchy and assume that they both implement some common protocol. If this protocol 
is not implemented by a common superclass, then each class must provide its own 
implementation, which results in code duplication. On the other hand, if we lift the 
implementation of the protocol up to the common superclass, we pollute the interface 
of the superclass, which affects all of its subclasses. Furthermore, if the protocol 
is defined by building on methods defined in intermediate classes, we will have to add 
these methods to the common superclass as well. This problem resul
ts from the dual nature of classes. Classes serve as both object generators and as 
superclasses. In the former case, the implementation of a class must be complete, 
whereas in the latter case the class implementation may have abstract methods that are 
implemented by a subclass. Traits provide a mechanism to separate these two aspects of 
classes and allow code to be reused across the class hierarchy. Multiple inheritance 
[Str94] and mixins [BC90, FKF98] represent two other attempts to solve this problem, 
but they both introduce semantic complexities and ambiguities (e.g., multiple copies 
of instance variables in the case of multiple inheritance, and problems with the order 
of method definition in the case of mixins) [SDNB03].

It is from link i provided in previous post.

-Dmitry Dorofeev.


Reply via email to