In the particular case where you have name conflicts, you'll receive a compile time error. Assuming D
is the implementing class:
class D extends A with C with B
def main(args: Array[String]): Unit = {
val d = new D
println(d.print())
}
You'll see:
Error:(25, 9) class D inherits conflicting members:
method print in trait B of type ()Unit and
method print in trait C of type ()Unit
(Note: this can be resolved by declaring an override in class D.)
class D extends A with B with C
However, If we help out the compiler by adding an override print()
to D
, and make it call super.print()
, it will print the last trait in the linage which support a print
method, i.e.:
trait A { }
trait B { def print() = println("hello") }
trait C { def print() = println("world") }
class D extends A with B with C {
override def print(): Unit = super.print()
}
We'd get "world". If we switched B
and C
:
class D extends A with C with B {
override def print(): Unit = super.print()
}
We'd get "hello".