What are the differences and similarties between Scala traits vs. Java 8 interfaces?

22,793

Solution 1

Motivations for Java 8' default methods and Scala traits differ.

The former was introduced to support safe API evolution and a limited form of multiple inheritance. With leveraging functional programming idioms in Project Lambda it's been beneficial to add, for example, a forEach(lambda) method to java.util.Collection interface without altering all possible implementers (which is actually impossible to do without breaking backward compatibility). As a side effect this also offered a form of mixin composition.

Scala traits were designed from scratch as building blocks for modular components composition. They are multiple inheritance friendly and don't have diamond problem by having strict rules on evaluation order of mix-ins due to linearization. They also support state, can reference the implementing class and place restrictions on which type can mix-in them. Look at Scala collections library where traits are used thoroughly.

Solution 2

Note that with scala 2.12.0 RC1 (Sept. 2016), Trait now compiles to an interface.
Scala 2.12 is all about making optimal use of Java 8’s new features

With Java 8 allowing concrete methods in interfaces, Scala 2.12 is able to compile a trait to a single interface.

Before, a trait was represented as a class that held the method implementations and an interface.
Note that the compiler still has quite a bit of magic to perform behind the scenes, so that care must be taken if a trait is meant to be implemented in Java.
(Briefly, if a trait does any of the following its subclasses require synthetic code: defining fields, calling super, initializer statements in the body, extending a class, relying on linearization to find implementations in the right super trait.)

See scala PR 5003 more the difference of implementation.

Solution 3

On similarity, both can have default methods,

Differences side, We can add traits to only instance, and not to entire class as well. Example:

trait A { def m = ??? }
class B 

new B() with A 

Like wise we can keep adding trait layers on an instance.

Share:
22,793

Related videos on Youtube

ams
Author by

ams

I love software development.

Updated on December 05, 2020

Comments

  • ams
    ams over 3 years

    I am new to Scala started learning the language for fun and I am still trying to get my head around it. My understanding of Scala traits is that they are like java interfaces except that some methods can have an implementation.

    Java 8 is adding interfaces that can have default methods where an implementation can be provided.

    What are the similarities and differences between Java 8 interfaces and Scala traits?

    • akki0996
      akki0996 almost 11 years
      check out that website, it might help you infoq.com/articles/java-8-vs-scala
    • Brice Roncace
      Brice Roncace over 9 years
      tl;dr: The primary motivator for virtual extension methods [default methods] is API evolution. A welcome side effect is that they offer a form of multiple inheritance, which is limited to behaviour. Traits in Scala not only provide multiple inheritance of behaviour, but also of state. Besides state and behaviour inheritance, traits offer a means to get a reference to the implementing class... [Scala] traits offer a richer set of features than [Java 8's] virtual extension methods.
    • Alonso del Arte
      Alonso del Arte over 2 years
      Have you thought about sealing one of the traits under the other?
  • Adelin
    Adelin over 4 years
    Can you elaborate more on this "Scala traits were designed from scratch as building blocks for modular components composition." ?