The Mystery of Traits in Scala
Crack the code — getting familiar with traits in Scala.
Join the DZone community and get the full member experience.
Join For FreeCrack 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.
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 not 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:
xxxxxxxxxx
trait Trait_Name{
//Fields
//Methods
}
For example:
xxxxxxxxxx
trait Calculator{
def doAddition(num1:Int,num2:Int) = {
num1+num2
}
def doSubtraction(num1:Int,num2:Int)
}
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 extends
or with
keywords.
class CalculatorOperation extends Calculator {
def doSubtraction(num1:Int,num2:Int):Int = {
num1-num2}
}
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:
xxxxxxxxxx
Class Class_Name extends TraitName-1 with TaitName-2 with TraitName-3{
//Body of Class
}
For example:
xxxxxxxxxx
trait A
{
def printMassage() { println("In trait A") }
}
trait B extends A {
override def printMassage() { println("In trait B") }
}
trait C extends A {
override def printMassage() { println("In trait C") }
}
class Alphabet extends C with B with A
object Alphabet extends App{
val obj =new Alphabet
obj.printMassage() }
Output: In trait B
Diagram: Inheritance hierarchy and linearization of class Alphabet
:
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!
Opinions expressed by DZone contributors are their own.
Comments