Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traits vs. Interfaces vs. Mixins?

What are the similarities & differences between traits, mixins and interfaces. I am trying to get a deeper understanding of these concepts but I don't know enough programming languages that implement these features to truly understand the similarities and differences.

For each of traits, mixins and interfaces

  • What is the problem being solved?
  • Is the definition of the concept consistent across programming languages?
  • What are the similarities between it and the others?
  • what are the differences between it and the others?
like image 510
ams Avatar asked Jun 03 '13 23:06

ams


People also ask

What is the difference between a trait and a mixin?

Traits are compile-time external values (rather than code generated from an external source). The difference is subtle. Mixins add logic, Traits add data such as compile-time type information.

Is mixin the same as interface?

Mixins are hailed as interfaces with behavioral reuse, more flexible interfaces, and more powerful interfaces. You will notice all these have the term interface in them, referring to the Java and C# keyword. Mixins are not interfaces. They are multiple inheritance.

What is the difference between trait and interface?

An interface is a contract that says “this object is able to do this thing”, whereas a trait is giving the object the ability to do the thing. A trait is essentially a way to “copy and paste” code between classes.

Are traits like interfaces?

Traits are interfaces Unlike interfaces in languages like Java, C# or Scala, new traits can be implemented for existing types (as with Hash above). That means abstractions can be created after-the-fact, and applied to existing libraries. Unlike inherent methods, trait methods are in scope only when their trait is.


1 Answers

Every reference type in Java, except Object, derives from one single superclass.

By the way, Java classes may implement zero or more interfaces.

Generally speaking, an interface is a contract that describes the methods an implementing class is forced to have, though without directly providing an implementation.

In other words, a Java class is obliged to abide its contract and thus to give implementation to method signatures provided by the interfaces it declares to implement.

An interface constitutes a type. So you can pass parameters and have return values from methods declared as interface types, requiring that way that parameters and return types implement particular methods without necessarily providing a concrete implementation for them.

This sets the basis for several abstraction patterns, like, for example, dependency injection.

Scala, on its own, has traits. Traits give you all the features of Java interfaces, with the significant difference that they can contain method implementations and variables. Traits are a smart way of implementing methods just once and - by means of that - distribute those methods into all the classes that extend the trait. Like interfaces for Java classes, you can mix more than one trait into a Scala class.

Since I have no Ruby background, though, I'll point you to an excerpt from David Pollak's "Beginning Scala" (amazon link):

Ruby has mixins, which are collections of methods that can be mixed into any class. Because Ruby does not have static typing and there is no way to declare the types of method parameters, there’s no reasonable way to use mixins to define a contract like interfaces. Ruby mixins provide a mechanism for composing code into classes but not a mechanism for defining or enforcing parameter types.

Interfaces can do even more than is described in this post; as the topic can be vast, I suggest you to investigate more in each one of the three directions, while if you even have Java background, Scala and therefore traits are affordable to learn.

like image 72
Federico Zancan Avatar answered Oct 05 '22 03:10

Federico Zancan