Say the following types exist:
public interface Base { default void sayHi(){ System.out.println("hi from base"); } } public interface Foo extends Base { @Override default void sayHi(){ System.out.println("hi from foo"); } } public interface Bar extends Base { } public class MyClass implements Foo, Bar { public static void main(String[] args) { MyClass c = new MyClass(); c.sayHi(); } }
In this case, if main is executed, "hello from foo" is printed. Why does the Foo implementation take precedence? Doesn't Bar inherit sayHi() from Base , since if MyClass to implement only Bar , would the Base implementation be implemented? Therefore, it would be wise for the code to still not compile. Also, since Bar must have a Base implementation of sayHi() , why can't I override it in MyClass , for example:
@Override public void sayHi() { Bar.super.sayHi(); }
When trying to do this, the following error occurs:
The bad type of the Bar classifier in the default supercall method, sayHi () is overridden in Foo
aiguy source share