Error
Duplicate default methods named [defaultMethod] with the parameters () and () are inherited from the types [DefaultMethodInterface2] and [DefaultMethodInterface] while using Default Method in Java 8
Error Type
Compile Time ( Java 8 )
Sample Code
DefaultMethodInterface
public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("Default Method");
}
}
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("Default Method 2");
}
}
HelloJava8
public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface dmi = new HelloJava8();
dmi.defaultMethod();
}
}
Cause
Class cannot implement multiple Interfaces having Default Methods with same Name and Signature.
Resolutions
Make Class implement only one Interface.
public class HelloJava8 implements DefaultMethodInterface {
public static void main(String[] args){
DefaultMethodInterface dmi = new HelloJava8();
dmi.defaultMethod();
}
}
or
Change the Method name in either of the Interface.
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
default public void defaultMethod2(){
System.out.println("Default Method 2");
}
}
Duplicate default methods named [defaultMethod] with the parameters () and () are inherited from the types [DefaultMethodInterface2] and [DefaultMethodInterface] while using Default Method in Java 8
Error Type
Compile Time ( Java 8 )
Sample Code
DefaultMethodInterface
public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("Default Method");
}
}
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("Default Method 2");
}
}
HelloJava8
public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface dmi = new HelloJava8();
dmi.defaultMethod();
}
}
Class cannot implement multiple Interfaces having Default Methods with same Name and Signature.
Resolutions
Make Class implement only one Interface.
public class HelloJava8 implements DefaultMethodInterface {
public static void main(String[] args){
DefaultMethodInterface dmi = new HelloJava8();
dmi.defaultMethod();
}
}
or
Change the Method name in either of the Interface.
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
default public void defaultMethod2(){
System.out.println("Default Method 2");
}
}