Content deleted Content added
No edit summary Tags: Manual revert Mobile edit Mobile web edit |
m linking |
||
Line 8:
For inter-object communication, traits are somewhere between an object-oriented [[Protocol (object-oriented programming)|protocol (interface)]] and a [[mixin]]. An interface may define one or more behaviors via [[Function prototype|method signatures]], while a trait defines behaviors via full method definitions: i.e., it includes the [[Class implementation file|body of the methods]]. In contrast, mixins include full method definitions and may also carry [[State (computer science)|state]] through member variable, while traits usually don't.
Hence an object defined as a trait is created as the [[function composition (computer science)|composition]] of methods, which can be used by other classes without requiring [[multiple inheritance]]. In case of a [[naming collision]], when more than one trait to be used by a class has a method with the same name, the programmer must explicitly disambiguate which one of those methods will be used in the class; thus manually solving the ''[[Multiple inheritance#The diamond problem|diamond problem]]'' of multiple inheritance. This is different from other composition methods in object-oriented programming, where conflicting names are automatically resolved by [[Scope (computer science)|scoping rules]].
Whereas mixins can be composed only using the [[Inheritance (object-oriented programming)|inheritance]] operation, traits offer a much wider selection of operations, including:<ref>{{cite journal
|