Unlock Scala in 5 Minutes: Traits vs Abstract Classes

jackwilltech

Jackson Williams

Posted on October 22, 2024

Unlock Scala in 5 Minutes: Traits vs Abstract Classes

1. Unlocking Scala Mastery: Understanding Abstract Classes and Traits

To master Scala, it's essential to grasp the fundamental difference between abstract classes and traits. Let's break down each concept to appreciate their unique characteristics.

Traits are similar to Java interfaces but offer more flexibility, as they allow the implementation of their members. In contrast, abstract classes are defined using the abstract keyword and can contain both abstract and concrete methods.

2. Shared Characteristics

Before diving into the differences, let's explore the commonalities between abstract classes and traits. This understanding will help us appreciate their distinct features.

A trait can include a declared and implemented method, along with a variable:

trait mytrait { 
  def myMethod():Unit
  val aVal: Int 
  val myVal = 10 
  def myImpMethod = { println("I am from trait") } 
}

An abstract class can feature both an abstract and a concrete method, along with a variable:

abstract class abstClass { 
  def myMethod():Unit 
  val aVal: Int 
  val myVal = 10 
  def myImpMethod:Unit = { println("I am from abstClass") } 
}

Notably, neither a trait nor an abstract class can be instantiated in the main method.

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

Classes that extend a trait or an abstract class must implement the declared methods.

class myTClass extends mytrait {
  val aVal=10 
  def myMethod ={ println("I am from myTclass") } 
} 
class myAClass extends abstClass {
  val aVal=10 
  def myMethod ={ println("I am from myAclass") } 
}

3. Key Differences

3.1 Constructor Parameters

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

The name serves as a constructor parameter. However, using a constructor parameter in a trait results in a compilation error, whereas it functions correctly in an abstract class. For more information on Scala mastery, visit t8tech.

3.2 Modifying Object Instances

scala trait objectInstanceTrait { def myObjectMethod = { println("I stem from the object instance trait") } } class myTClass {} def main(args: Array[String]): Unit = { val classObjWithTrait = new myTClass with objectInstanceTrait classObjWithTrait.myObjectMethod } output => "I stem from the object instance trait"

The function “myObjectMethod” defined in the trait can be invoked via an instance of a class. An abstract class cannot be integrated into an object instance, leading to a compilation error.

3.3 Multiple Inheritances

In the realm of multiple inheritance, a single class can derive from more than one superclass, inheriting traits from all parent classes. Although Scala does not allow multiple inheritance with classes, it can be achieved using traits.

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

Employing an abstract class for multiple inheritances results in a compilation error.

Scala resolves the diamond problem in multiple inheritance through trait linearization, where the trait located furthest to the right takes precedence.

trait Printer { 
  def print(msg: String) = println(msg) 
} 
trait DelimitWithHyphen extends Printer { 
  override def print(msg: String) = { println("-------------") } 
} 
trait DelimitWithStar extends Printer { 
  override def print(msg: String) = { println("*") } 
} 

class CustomPrinter extends Printer with DelimitWithHyphen with DelimitWithStar 

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

3.4 Stackability

Stackable traits in Scala facilitate the amalgamation of multiple traits that jointly alter a method. This procedure entails invoking super.theMethod.

html

trait foundation { 
  def foundationMethod(s: String): Unit = println(s) 
}

trait stackA extends foundation {
  override def foundationMethod(s: String): Unit = println("from stackA") 
} 

trait substituteA extends foundation { 
  abstract override def foundationMethod(s: String): Unit = {
    super.foundationMethod(s); 
    println("from substituteA")
  } 
}

class Stackable { 
  this: foundation => 
  def apply() { 
    println(foundationMethod("base")) 
  } 
} 

(new Stackable with stackA with substituteA)() 

output => 
from stackA 
from substituteA

The invocation initiates with substituteA, which subsequently directs to stackA through super.foundationMethod(s).

This method invocation reaches stackA as it is situated to the left of substituteA.

3.5 Compatibility with Java

Traits can integrate effortlessly with Java provided they do not contain any implementation; abstract classes can be directly employed.

4. Summary

Scala traits provide enhanced versatility in comparison to abstract classes. Abstract classes should be utilized when a base class with a constructor parameter is required. In situations of doubt, it is prudent to choose traits, as they can be readily converted into abstract classes.

💖 💪 🙅 🚩
jackwilltech
Jackson Williams

Posted on October 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related