Scala Tutorial

Traits

Mixins, abstract contracts, and multiple inheritance in Scala

What is a Trait?

A Trait in Scala is similar to a Java interface but much more powerful. It can contain both abstract methods and implemented methods. Traits are used to share interfaces and fields between classes.
scala
trait Logger {
  def log(msg: String): Unit = println(s"[LOG]: $msg")
}

class User extends Logger {
  def save(): Unit = log("Saving user...")
}

Multiple Mixins

Wait! Unlike classes (which only support single inheritance), a class can mix in multiple traits using the with keyword.
scala
class Admin extends User with Logger with Authenticator

Abstract vs. Concrete

  • Abstract Method: Only the name and type are defined.
  • Concrete Method: Has a full implementation.
Traits are perfect for "Stackable Modifications," where multiple traits modify the behavior of a method in a specific order.

Why use Traits?

  1. Modularity: Break down complex behaviors into small, reusable components.
  2. Flexible Design: Add functionality to a class without forcing it into a specific inheritance tree.
  3. Composition: Favor composition over deep inheritance.

Summary

TermDescription
traitKeyword to define a trait
extendsUsing the first trait
withAdding additional traits
overrideRequired when overriding a concrete method
MixingThe process of adding a trait to a class
Best ForHorizontal code reuse and defining contracts
Logic"What an object can DO"
SafetyCompiler ensures all abstract methods are implemented
ModernScala 3 traits can also have constructor parameters
StandardUse PascalCase for all trait names
IdentityTraits cannot be instantiated on their own