Object.clone() only works if the Cloneable interface is implemented, otherwise it throws an exception. If Cloneable is implemented then Object.clone() basically performs a memory copy, as far as I understand.
So, why is clone() defined in Object at all? Wouldn't it make a lot more sense to transfer the clone() default implementation to the Cloneable interface and have Object not mention clone() at all?
The only difference to the current specification would be that, instead of a runtime exception, you would get a compile-time error when attempting to clone a non-Cloneable object. Which would make a lot more sense.
Am I missing something?
Wouldn't it make a lot more sense to transfer the clone() default implementation to the Cloneable interface
clone()is a low-level
nativemethod and interfaces cannot declare
defaultmethods, which is to provide convenience API that can be expressed purely in terms of other interface methods.
A fair amount of general reasoning can also be found in this answer.