DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  1. DZone
  2. Coding
  3. Languages
  4. The Mystery of Traits in Scala

The Mystery of Traits in Scala

Crack the code — getting familiar with traits in Scala.

By 
Praful Bangar user avatar
Praful Bangar
·
Updated Mar. 26, 20 · Presentation
Likes (6)
Comment
Save
Tweet
Share
31.8K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

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:

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!

Trait (computer programming) Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook