I came across following paragraph while reading about java 8 default methods from here:
If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods.
I quickly tried following code to confirm the behavior
public class DefaultMethodClass {
public void defaultMethod()
{
System.out.println("DefaultMethodClass.defaultMethod()");
}
}
public interface DefaultMethodInterface {
public default void defaultMethod()
{
System.out.println("DefaultMethodInterface.defaultMethod()");
}
}
public class DefaultMethodClassInterfaceChild extends DefaultMethodClass implements DefaultMethodInterface
{
public static void main(String[] args) {
(new DefaultMethodClassInterfaceChild()).defaultMethod();
}
}
This prints
DefaultMethodClass.defaultMethod()
I am not able to see any specific reason behind why this particular behavior is chosen by language designer. Is their any such significant reason that I am missing? Or its just that interface default method logically bears lesser importance than concrete implementation provided by the super class?