Traits
Traits are Scala’s flexible building blocks for sharing behaviour — like Java interfaces with more power.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
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#
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")
[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#
- Notifier declares the contract.
- Logging and Uppercase wrap send with super calls.
- Uppercase (rightmost) runs first, then Logging.
- 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#
Keep traits small and focused, like interfaces.
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
trait Greeter:
def greet(name: String) = s"Namaste, $name"
class Shop extends Greeter
Shop().greet("Asha")
Mini quiz#
How many traits can a class mix in?
Want more? Practise all Scala MCQs · Scala interview questions
My notes#
Saved in this browser.