Error
"A default method cannot override a method from java.lang.Object" while using Default Method in Java 8.
Error Type
Compile Time ( Java 8 )
Sample Code
public interface DefaultMethodInterface {
static public void toString(){
System.out.println("DefaultMethodInterface");
}
}
Cause
The specified Default method has the same name and signature as of Object Class method. We cannot override a Object Class method with the default method in Interface.
Resolution
Change the Name of the default method or remove it.
public interface DefaultMethodInterface {
static public void string(){
System.out.println("DefaultMethodInterface");
}
}
Why is it forbidden to override an Object Class method with Default Methods ?
"A default method cannot override a method from java.lang.Object" while using Default Method in Java 8.
Error Type
Compile Time ( Java 8 )
Sample Code
public interface DefaultMethodInterface {
static public void toString(){
System.out.println("DefaultMethodInterface");
}
}
Cause
The specified Default method has the same name and signature as of Object Class method. We cannot override a Object Class method with the default method in Interface.
Resolution
Change the Name of the default method or remove it.
public interface DefaultMethodInterface {
static public void string(){
System.out.println("DefaultMethodInterface");
}
}
Why is it forbidden to override an Object Class method with Default Methods ?