27

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?
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ams
  • 60,316
  • 68
  • 200
  • 288
  • 1
    Scala has traits only (mixins and interfaces would be encompassed by traits). Looking at your profile it seems you have a Java background. You should try Scala, you'll find out that traits are very convenient when you want to mix-in some implementation that is orthogonal to your current class. It's harder to do right when you want to stack traits. See http://www.artima.com/pins1ed/traits.html for more details. – huynhjl Jun 04 '13 at 06:42
  • 1
    @ams: Have a look at this [answer](http://stackoverflow.com/a/23124968/33311). Might be useful even if it's tagged "groovy". – Lernkurve Dec 09 '14 at 13:35

1 Answers1

10

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.

ian
  • 12,003
  • 9
  • 51
  • 107
Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
  • You should clarify that every *refererence* type derives from Object. The primitives do not. I would remove the sentence "As it is said widely around the Internet..." or provide some links to well known articles. – coltfred Jun 04 '13 at 12:14
  • I disagree with Pollak on, firstly "there’s no reasonable way to use mixins to define a contract like interfaces" - [a contract is *primarily* about *methods*](https://www.eiffel.com/values/design-by-contract/introduction/), so it does. Secondly "not a mechanism for defining or enforcing parameter types" - Ruby uses duck typing and method naming for this. See [this answer for more](http://stackoverflow.com/a/177127/335847) – ian Jan 10 '15 at 18:38