Wednesday, February 4, 2015
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 methods
  def make() : String
  def speed() : Int
  // Concrete methods
  def 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 Car
    c.specs
  }
}

This program produces the following output.

Ford
150

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 Derived3
    d.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.

7 comments :

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.


    SAP training in Chennai

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

    Digital marketing company in Chennai

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top