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 {
public void defaultMethod(){
}
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 Methods with same Name and Signature even though we have declared one as Default.
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");
}
}
or
Make the methods in both the interfaces as Non Default and remove the default definition.
DefaultMethodInterface
public interface DefaultMethodInterface {
public void defaultMethod();
}
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
public void defaultMethod();
}
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 {
public void defaultMethod(){
}
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 Methods with same Name and Signature even though we have declared one as Default.
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");
}
}
or
Make the methods in both the interfaces as Non Default and remove the default definition.
DefaultMethodInterface
public interface DefaultMethodInterface {
public void defaultMethod();
}
DefaultMethodInterface2
public interface DefaultMethodInterface2 {
public void defaultMethod();
}