Taking the following class and interface:
public class A {
public int foo() {
return 1;
}
}
public interface B {
default int foo() {
return 2;
}
}
If you define a sublclass in java like that
public class C extends A implements B {
}
Calling foo on an instance of C will return 1, as java prefers the superclass over the interface.
Doing the same thing in scala gives a compile error:
class D extends A with B
Error:(3, 9) class D inherits conflicting members:
method foo in class A of type ()Int and
method foo in trait B of type ()Int
(Note: this can be resolved by declaring an override in class D.)
Manually overriding the method
class D extends A with B {
override def foo(): Int = super.foo()
}
Still gives incorrect behaviour, calling foo on an instance of D will return 2.
To get a scala class to behave the same way as java in this case you have manually override every single method and specify A as the superclass in every call.
class D extends A with B {
override def foo(): Int = super[A].foo()
}
Why was it implemented like that? And is there a reasonable way to work around it, without manually overriding every method in that situation?