Reputation: 563
I have recently used default methods in java. In its implementation I found
public interface DefaultMethod {
default String showMyName(String name){
return "Hai "+name;
}
}
public class DefaultMethodMainImpl implements DefaultMethod{
@Override
public String showMyName(String name){
return DefaultMethod.super.showMyName(name);
}
}
My question is in DefaultMethod.super where super will call it have no super class except Object? what super will return?
Upvotes: 23
Views: 18111
Reputation: 14762
JLS 3.9.: super
is a keyword.
JLS 15.12.1. Compile-Time Step 1: Determine Class or Interface to Search
If the form is TypeName
.
super
.
[TypeArguments] Identifier, then:
...
Otherwise, TypeName denotes the interface to be searched, I.
Let T be the type declaration immediately enclosing the method invocation. It is a compile-time error if I is not a direct superinterface of T, [...]
| ... |
| To support invocation of default methods in superinterfaces,
the TypeName may also refer to a direct superinterface
of the current class or interface, and the target is that superinterface. |
N.B.: JLS 15.8.3. this
When used as a primary expression, the keyword
this
denotes a value that is a reference to the object for which the [...] default method was invoked, [...][...]
The type of
this
is the [...] interface type T within which the keywordthis
occurs.
Upvotes: 0
Reputation: 1610
If you use super
in a class
it usually refers to the ancestor of that class (either the extend
ed class or Object
).
In the case of:
default
method of an interface
default
method with the same signatureyou have to specify the specific interface which default implementation you want to invoke, hence:
<Interface>.super.<method>();
See also The diamond problem.
Upvotes: 42
Reputation: 6087
Consider this code:
public interface One {
default void method() {
System.out.println("method from One");
}
}
interface Two extends One {
default void method() {
System.out.println("method from Two");
}
}
class Three implements Two {
public void method() {
Two.super.method();
}
public static void main(String[] args) {
new Three().method();
}
}
Output: method from Two
Two.super.method()
is just a clumsy syntax to avoid the diamond problem explained in another answer.
As you can see from the program output, super
not really means "calling to ancestor" as it used to be in Java.
Upvotes: 2
Reputation: 119
The main reason is that there could be more than one interface with a super level having a default method with the same method signature. The java compiler needs to understand what Interface is referring to here.
See the below example.
public interface ABC {
default String showMyName(String name) {
return "Hai " + name;
}
}
public interface CDE {
default String showMyName(String name) {
return "Hey.........!!! " + name;
}
}
public class DefaultMethodMainImpl implements ABC, CDE {
@Override
public String showMyName(String name) {
System.out.println(ABC.super.showMyName(name));
System.out.println(CDE.super.showMyName(name));
return ABC.super.showMyName(name);
}
}
Upvotes: 2
Reputation: 131
<Interface>.<method>();
would consider <method>
as static, and this is not the case. Hence the use of key word super
which is a reference variable that is used to refer a "parent" object.
Upvotes: 13