Scala · OOP & Functional Programming

Traits

Traits are Scala’s flexible building blocks for sharing behaviour — like Java interfaces with more power.

Intermediate 5 min Lesson 14 of 25Examples use Scala 3 (3.3 LTS or newer) with sbt or scala-cli.
Before you start:Classes and Objects
0/25 in Scala
On this page · 5 min · Intermediate

In simple words#

Traits are skill badges you stitch onto a uniform: a Logger badge, a Retry badge. A class can wear many badges and gains every skill they bring.

trait
Reusable set of methods and fields.
Mixin
Adding traits with extends … with ….
Linearization
The order in which mixed-in traits are applied.

Learning objectives#

  • Define traits with abstract and concrete methods
  • Mix in multiple traits
  • Understand stackable traits
  • Choose traits vs abstract classes

Explanation#

A trait can declare abstract methods and provide concrete ones.

class A extends B with C with D mixes in several traits.

Traits can call super to stack behaviour; the rightmost trait runs first (linearization).

Scala 3 traits can take parameters; sealed traits define closed hierarchies.

Code example: Mixing notification behaviours#

ScalaMixing notification behaviours Illustrative
trait Notifier:
  def send(to: String, msg: String): String


trait Logging extends Notifier:
  abstract override def send(to: String, msg: String): String =
    val result = super.send(to, msg)
    println(s"[log] $result")
    result


trait Uppercase extends Notifier:
  abstract override def send(to: String, msg: String): String =
    super.send(to, msg.toUpperCase)


class SmsNotifier extends Notifier:
  def send(to: String, msg: String): String = s"SMS to $to: $msg"


val n = new SmsNotifier with Logging with Uppercase
n.send("+91 98xxxxxx01", "order shipped")
Expected output
[log] SMS to +91 98xxxxxx01: ORDER SHIPPED

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Notifier declares the contract.
  2. Logging and Uppercase wrap send with super calls.
  3. Uppercase (rightmost) runs first, then Logging.
  4. Behaviour is composed without changing SmsNotifier.

Real-world use case#

Cross-cutting concerns such as logging, retry and metrics are added to services as stackable traits.

Common mistakes#

Huge traits

Keep traits small and focused, like interfaces.

Confusing mixin order

Remember: rightmost trait’s method runs first.

Best practices#

  • Small focused traits
  • sealed traits for ADTs
  • Prefer composition
  • Document stacking order

Remember this#

trait = interface + code.

extends A with B with C.

Rightmost mixin runs first.

Practice exercise#

Your task: Create a trait Greeter with a concrete greet(name) method and mix it into a class.

Show solution
Scala Illustrative
trait Greeter:
  def greet(name: String) = s"Namaste, $name"


class Shop extends Greeter
Shop().greet("Asha")

Mini quiz#

Traits quizMultiple choice Question 1 of 1

How many traits can a class mix in?

Score so far: 0/0

Want more? Practise all Scala MCQs · Scala interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.