When traits are combined with classes to create extended behaviour, this process is known as mixin and the traits are said to be mixed-in or mixins.
The example below shows the behaviour of the plain traits class using its putMsg method, and then its behaviour when mixed-in with TraitAddA, TraitAddA and TraitAddB and then with TraitAddA and TraitAddB reversed (ordering of the with parts is significant).
As well as mixin with the new keyword, class TraitABClass shows that traits can be used as mixins with normal classes too, at the point they are declared using the with keyword.
Example Scala file showing the operation of Traits:
package test
object TraitsTest {
def main(args : Array[String]) {
val traits : Traits = new Traits()
traits.putMsg("Hello!")
println("traits, value = " + traits.toString)
val traitsA : Traits = new Traits() with TraitAddA
traitsA.putMsg("Hello!")
println("traitsA, value = " + traitsA.toString)
val traitsAB : Traits = new Traits() with TraitAddA with TraitAddB
traitsAB.putMsg("Hello!")
println("traitsAB, value = " + traitsAB.toString)
val traitsBA : Traits = new Traits() with TraitAddB with TraitAddA
traitsBA.putMsg("Hello!")
println("traitsBA, value = " + traitsBA.toString)
val traitsABClass : TraitsABClass = new TraitsABClass
traitsABClass.putMsg("Hello2")
println("traitsABClass, value = " + traitsABClass.toString)
}
}
class TraitsABClass extends Traits with TraitAddA with TraitAddB
class Traits {
var msg : String = ""
def putMsg(m : String) {
msg = m
}
override def toString = {
msg
}
}
trait TraitAddA extends Traits {
abstract override def putMsg(m : String) = {
super.putMsg("A" + m)
}
}
trait TraitAddB extends Traits {
abstract override def putMsg(m : String) = {
super.putMsg("B" + m)
}
}
Program execution results:
traits, value = Hello!
traitsA, value = AHello!
traitsAB, value = ABHello!
traitsBA, value = BAHello!
traitsABClass, value = ABHello2
.