In the specific case, when you have name conflicts, you will get a compile-time error. Assuming D
is an implementing class:
class D extends A with C with B def main(args: Array[String]): Unit = { val d = new D println(d.print()) }
You will 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 the compiler by adding override print()
to D
and calling it super.print()
, it will print the last sign in the line that supports the print
method, that is:
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 will get the "world". If we switched B
and C
:
class D extends A with C with B { override def print(): Unit = super.print() }
We would get a hello.
source share