3

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?

MsA
  • 2,599
  • 3
  • 22
  • 47
  • 2
    See also [Why is it forbidden to define a default method for a method from java.lang.Object?](https://stackoverflow.com/q/24016962/2711488) – Holger Jul 25 '18 at 14:00

2 Answers2

7

I am not able to see any specific reason behind why this particular behavior is chosen by language designer.

Method definitions were in Java 1.0, Interface default methods where added in Java 8. Code written for Java 1.0 still needs to work.

Or its just that interface default method logically bears lesser importance than concrete implementation provided by the super class?

Effectively they have lesser importance being added after 20+ years of existing code being written.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 6
    Even without backward compatibility to pre-Java 8 code, allowing `default` methods to override existing methods could lead to surprising behavior of existing code, when a `default` method is added to an interface. – Holger Jul 25 '18 at 13:59
7

Answer is simple: you are allowed to implement multiple interface but you can just extend one class. Indeed you can't have multiple default methods with the same name by implementing multiple interfaces.

If in any circumstance a default method has higher priority than a method implemented inside a real class in the hierarchy you end up having the same exact problem for which Java designers didn't want multiple inheritance: ambiguity.

As a personal opinion: a default method is seen as a "implementation of a functionality when none is defined", if the method is defined in a real class it is expected to be a more specific implementation.

Jack
  • 131,802
  • 30
  • 241
  • 343