Trait (computer programming): Difference between revisions

Content deleted Content added
m Reverted edits by 120.21.5.252 (talk) to last version by Narky Blert
Line 6:
Traits both provide a set of methods that implement behaviour to a class, and require that the class implement a set of methods that [[Parameter (computer programming)|parameterize]] the provided behaviour.
 
For inter-object communication, traits are somewhatsomewhere 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 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]].