I came over a strange behavior of the Eclipse compiler today and I'm not sure what to think of it. We're trying to create a useful Cloneable
interface like that:
public interface PublicCloneable extends Cloneable {
Object clone();
static <T extends PublicCloneable> T clone(final T obj) {
if (obj != null) {
return (T) obj.clone();
}
return null;
}
}
The fun part is that the compiler complains about obj.clone()
: Unhandled exception type CloneNotSupportedException
I know how to fix it, we can just cast obj
to PublicCloneable
and be done with it. But what I'm interested in: why would the compiler prefer the method of Object
to a method of an implementation?