Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

scala

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?

like image 482
ams Avatar asked May 07 '13 02:05

ams


People also ask

What is the difference between Java interface and Scala trait?

Traits in Scala have a lot of similarities with interfaces in Java, but a trait is more powerful than an interface because it allows developers to implement members within it. The trait is a combination of abstract and non-abstract methods. Trait can not be instantiated, thus it has no parameters.

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.

What is traits in Scala?

A Trait is a concept pre-dominantly used in object-oriented programming, which can extend the functionality of a class using a set of methods. Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters.

Is Scala trait same as interface?

Like a class, Traits can have methods(both abstract and non-abstract), and fields as its members. Traits are just like interfaces in Java. But they are more powerful than the interface in Java because in the traits we are allowed to implement the members.


2 Answers

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.

like image 154
2 revs, 2 users 93% Avatar answered Oct 29 '22 15:10

2 revs, 2 users 93%


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.

like image 42
VonC Avatar answered Oct 29 '22 14:10

VonC