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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Mystery of Traits in Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Coding
  3. Languages
  4. The Practical Difference Between Abstract Class and Trait in Scala

The Practical Difference Between Abstract Class and Trait in Scala

Take a look at what the actual differences are between abstract class and trait in Scala.

By 
vivek kale user avatar
vivek kale
·
Jun. 22, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
5.3K Views

Join the DZone community and get the full member experience.

Join For Free

1. Overview

Before getting to know the difference between abstract class and trait lets understand them.

Traits are like Java interface but in a trait, we are allowed to implement its members.

Abstract Classes are constructed using abstract keyword, it supports both abstract and non-abstract methods.

2. Similarities

Once we understand similarities then only we can appreciate the differences. Let's go ahead and learn those.

A trait with a declared and implemented method and variable:

Scala
 




xxxxxxxxxx
1


 
1
trait mytrait { 
2
  def myMethod():Unit
3
  val aVal: Int 
4
  val myVal = 10 
5
  def myImpMethod = { println("I am from trait") } 
6
}


An abstract class with an abstract and non-abstract method and variable:

Scala
 




xxxxxxxxxx
1


 
1
abstract class abstClass { 
2
  def myMethod():Unit 
3
  val aVal: Int 
4
  val myVal = 10 
5
  def myImpMethod:Unit = { println("I am from abstClass") } 
6
}


In the main method, trait and abstract class both can't be instantiated.

Scala
 




xxxxxxxxxx
1


 
1
val myTObj = new mytrait //compilation error
2
val myAObj = new abstClass() //compilation error


Classes that are extending trait or abstract class need to implement the declared methods.

Scala
 




xxxxxxxxxx
1


 
1
class myTClass extends mytrait {
2
  val aVal=10 
3
  def myMethod ={ println("I am from myTclass") } 
4
} 
5
class myAClass extends abstClass {
6
  val aVal=10 
7
  def myMethod ={ println("I am from myAclass") } 
8
}


3. Differences

3.1 Constructor Parameters

Scala
 




xxxxxxxxxx
1


 
1
trait myTraitWithParam(name: String){} 
2
abstract class abstClassWithParam(name : String){}


name is the constructor parameter, a trait with a constructor parameter gives compilation error whereas it works fine with an abstract class.

3.2 Adding to Object Instance

Scala
 




xxxxxxxxxx
1


 
1
trait objectInstanceTrait { 
2
  def myObjectMethod = { println("I am from object instance trait") } 
3
} 
4
class myTClass {} 
5
def main(args: Array[String]):Unit={ 
6
  val classObjWithTrait = new myTClass with objectInstanceTrait
7
  classObjWithTrait.myObjectMethod 
8
} 
9
ouptput => "I am from object instance trait"


The implemented method "myObjectMethod" from the trait can be accessed by an instance of a class. Abstract class can't be added to an object instance, it leads to a compilation error.

3.3 Multiple Inheritances

In multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Scala does not support multiple inheritance with classes, but it can be achieved by traits.

Scala
 




xxxxxxxxxx
1


 
1
class myClass extends trait1 with trait2{} 
2
class myNewClass extends abstClass1 with abstClass2{}


When abstract class used for multiple inheritances, it results in a compilation error.

With multiple inheritance, the scala resolves the diamond problem, by trait linearization. A trait with the extreme right will get the highest priority.

Scala
 




xxxxxxxxxx
1
15


 
1
trait Printer { 
2
  def print(msg : String) = println (msg) 
3
 } 
4
trait DelimitWithHyphen extends Printer { 
5
  override def print(msg : String)= { println("-------------") } 
6
} 
7
trait DelimitWithStar extends Printer { 
8
  override def print(msg : String)= { println("*************") } 
9
} 
10

          
11
class CustomPrinter extends Printer with DelimitWithHyphen with DelimitWithStar 
12

          
13
new CustomPrinter().print("Hello World!") 
14
  
15
 output=> "*************"


3.4 Stackability

Stackable traits in Scala refers to being able to mix in multiple traits that work together to apply multiple modifications to a method. This involves invoking super.theMethod

Scala
 




xxxxxxxxxx
1
22


 
1
trait base{ 
2
  def baseMethod(s: String):Unit = println(s) 
3
}
4

          
5
trait stack1 extends base{
6
  override def baseMethod(s: String) = println("from stack1") 
7
} 
8
trait replace1 extends base { 
9
  abstract override def baseMethod(s: String) ={
10
    super.baseMethod(s); 
11
    println("from replace1")} 
12
}
13
class Stackable { 
14
  this : base => def apply() 
15
  { println( baseMethod("bottom") ) } 
16
} 
17

          
18
(new Stackable with stack1 with replace1)() 
19

          
20
output => 
21
from stack1 
22
from replace1


Invocation started from relace1 then it is passed to stack1 by super.baseMethod(s).

This method invocation went to stack1 because it is left side of replace1.

3.5 Interoperable with Java

Traits are interoperable with Java if they don't have any implementation, abstract classes can be directly used.

4. Conclusion

Scala traits are more flexible than abstract classes. We should use abstract classes when we want to create a base class with a constructor argument. In case of dought always go with the trait, because traits can be easily converted into an abstract class.

Trait (computer programming) Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • The Mystery of Traits in 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!