When creating a default method in Java 8, certain Object
methods are not callable from within the default method. For example:
interface I {
default void m() {
this.toString(); // works.
this.clone(); // compile-time error, "The method clone() is undefined for the type I"
this.finalize(); // same error as above.
}
}
It seems that clone()
and finalize()
are the only methods from Object
that are not allowed. Coincidently, these are the only methods of Object
that are protected, but this question is in particular regard to default methods, as they will be inherited by classes that do extend java.lang.Object
. What is the reason for this?