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 ?
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 overriden default
method of an interface
you have to specify the specific interface which default implementation you want to invoke, hence
<Interface>.super.<method>();
See also The diamond problem .
<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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.