{{announcement.body}}
{{announcement.title}}

The Mystery of Traits in Scala

DZone 's Guide to

The Mystery of Traits in Scala

Crack the code — getting familiar with traits in Scala.

· Java Zone ·
Free Resource

Crack the code — getting familiar with traits in Scala.

In this blog, we are getting familiar with traits in Scala and try to gather some knowledge on how to implement them in code. We will understand the trait in a better way by comparing it with Java language features like interfaces and abstract classes.

You may also like: Scala Traits: Let's Mix It Up a Bit

Introduction to Traits

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 be instantiated, thus it has no parameters. A trait can be extended by other traits, abstract classes, concrete classes, and case classes as well.

The trait definition looks like a class definition with a “trait” keyword.

Syntax:

Scala




xxxxxxxxxx
1


 
1
trait Trait_Name{
2
          //Fields
3
          //Methods
4
      }



For example:

Scala




xxxxxxxxxx
1


 
1
trait Calculator{
2
def doAddition(num1:Int,num2:Int) = {
3
num1+num2
4
}
5
def doSubtraction(num1:Int,num2:Int)
6
}



In the given example, the trait Calculator is defined with the concrete method  doAddition and the abstract method doSubtraction.

Once a trait is defined, it can be mixed in the class by using extendsor with keywords.

Scala




x


 
1
class CalculatorOperation extends Calculator {
2
def doSubtraction(num1:Int,num2:Int):Int = {
3
num1-num2}
4
}



After this, you can call methods in trait and class by creating an object of class  CalculatorOperation in the singleton object or in the main method.

Inheritance Using Trait

As Interfaces in Java are used to achieve Multiple Inheritance similarly Traits are used to achieve Multiple Inheritance in Scala. When a class inherits multiple traits we use ‘extends’ keyword before the first trait and after that use ‘with’ keyword before other traits. Traits can be used to achieve Multiple Inheritance in Scala, so the Diamond problem of Multiple Inheritance solved by linearization using traits.

Syntax:

Scala




xxxxxxxxxx
1


 
1
Class Class_Name extends TraitName-1 with TaitName-2 with TraitName-3{
2
//Body of Class
3
}



For example:

Scala




xxxxxxxxxx
1
16


 
1
trait A
2
{
3
 def printMassage() {   println("In trait A") }
4
 }
5
trait B extends A { 
6
 override def printMassage() {   println("In trait B") }
7
}
8
trait C extends A {
9
 override def printMassage() {   println("In trait C") }
10
}
11
 
          
12
class Alphabet extends C with B with A
13
 
          
14
object Alphabet extends App{
15
 val obj =new Alphabet
16
 obj.printMassage()  }



Output: In trait B

Diagram: Inheritance hierarchy and linearization of class Alphabet:

Image title


Now, we will see the differences between abstract classes and traits:

Trait

Abstract Class

Traits do not have a constructor.

Abstract class contain a constructor

A Class can extend multiple traits.

A Class can extend only one Abstract class.

A trait can be added to Object Instance.

We can’t add Abstract Class to Object Instance.

Conclusion

In this blog, we learned the basic implementation of traits and how traits work in several idioms. You saw that traits are similar to interfaces as well as abstract classes, and traits are more flexible and powerful than them both. Hopefully, you have learned some basics of one of the most used building blocks in Scala OOP.

Thanks for reading!

Further Reading

Scala Traits: Let's Mix It Up a Bit

A Journey With Scala

Topics:
scala ,trait ,java ,oop ,oop in scala ,abstract ,interface

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}