Scala Traits

Scala Traits defines object types by providing signature of the supported methods. This tutorial provides a quick introduction to Scala Traits.

Some key points on Scala Traits.
  1. Scala Traits are like interfaces in Java. 
  2. Scala Traits can have default implementations for certain methods unlike Java interfaces. 
  3. Scala Traits don’t have any constructor parameters.
  4. Scala class inherits only one super class but supports mixing multiple traits.

Let us look into a simple example to understand Scala Traits. Scala Traits are defined using the keyword trait and looks like a class definition. In this example we define a trait called Vehicle which can have both abstract methods and concrete methods.

trait Vehicle {// Abstract methodsdef make() : Stringdef speed() : Int// Concrete methodsdef specs() = {println(make)println(speed)}}

Let us write a class which extends the trait Vehicle. Concrete implementation needs to be provided only for the traits abstract methods.

class Car extends Vehicle {def make() : String = { "Ford" }def speed() : Int = { 150 }}object ScalaTraitsTest {def main(args:Array[String]) {val c = new Carc.specs}}

This program produces the following output.


Scala Traits and Diamond Problem

For C++ programmers whenever topic of multiple inheritance is discussed the focus is on the topic of diamond problem. Here classes Derived1 and Derived2 inherit from Base and Derived3 inherits from both Derived1 and Derived2. If both Derived1 and Derived2 override a method in Base there is ambiguity in Derived3 on which implementation should be inherited. In C++ developers need to override each conflicted method at the bottom of the diamond to resolve the ambiguity.
Diamond Problem with Multiple Inheritance
Java doesn’t support multiple inheritance prior to Java 8 and hence avoid the diamond problem. But in Java 8 with the introduction of default methods on interfaces Java is subject to diamond problem. Again the conflicted method needs to be implemented at the bottom of diamond to resolve ambiguity.
Scala provide a simple solution to resolve the diamond problem by considering the order in which Traits are inherited. The implementation in the super type that is furthest to the right is considered. If the class/ trait inheriting provides a overridden implementation then this is considered.
Let us analyze Scala solution to Diamond problem with an example.
trait Base {def print}trait Derived1 extends Base {override def print() = { println("I am Derived1") }}trait Derived2 extends Base {override def print() = { println("I am Derived2") }}class Derived3 extends Derived1 with Derived2 {}object DiamondTest {def main(args:Array[String]) {val d = new Derived3d.print}}

Here the super type Derived2 is farthest to the right and produces the following output.

I am Derived2
If the clsas Derived3 has its own implementation for the method as below.

class Derived3 extends Derived1 with Derived2 {override def print() = { println("I am Derived3") }}

Output is.

I am Derived3

Proceed to read other Scala Tutorials.