It seems you are a little confused by the presence of the default method. So let's forget for a moment that IFace1.printHello() is the default method. So, there is a clear situation: Test implements two interfaces: IFace1 and IFace2 , which have a method with the same name and signature.
Test implements this method and therefore implements the method for both interfaces. The new default method function does not change anything about this logic. Moreover, the language developers made sure that the addition of the default method did not affect the behavior of existing code, so if your class implements the method, the presence of default methods becomes inappropriate.
However, if you write code that knows about the existence of default methods, you can call it if it is declared or inherited by a direct super-interface, that is, in your code you can use IFace1.super.printHello() to call the default IFace1 method.
The rules are not much different from the rules of superclasses. If you change the interfaces so that IFace2 expands IFace1 and still declares printHello() as an abstract method, this abstract method overrides the default method, and you cannot call IFace1.super.printHello() from inside Test anymore.
As said, these rules are not much different from ordinary instance methods. If Test declares a printHello() method, this is the only method that you can invoke with a reference to the Test instance, whether its declared type is Test , IFace1 or IFace2 . Only Test implementation methods can perform super invocations.
The main difference comes into play when possible multiple inheritance of interfaces is involved. If your Test class does not implement the printHello() method, it depends on the inheritance relationship of the two interfaces, which will happen
- If
IFace2 extends IFace1 , its abstract method overrides the default method, so a compiler error occurs because Test must implement the abstract method - If
IFace2 does not extend to IFace1 , there are ambiguously two inherited methods with the same name and signature, therefore Test does not inherit the default method and a compiler error occurs, because Test must implement the abstract method - If
IFace1 extends IFace2 , Test inherits the default method. It also inherits it if Test does not implement IFace2 , but this should come as a surprise ...
source share