The Java specification for the java.lang.Cloneable
interface defines itself as meaning that any object that extends it also implements the clone()
method, which remains inactive in java.lang.Object
. In particular, he says that:
The class implements the Cloneable
interface to indicate to the java.lang.Object#clone()
method that it is legal for this method to instantiate this class for the field.
For me, this means that we should assume that every class that extends Cloneable
therefore also has a public Object clone()
method in it. This makes it easy to assume that a valid method is:
public static makeACloneFrom(Cloneable c) { return c.clone(); }
however, this is not the case since the entire source code of Cloneable
(sans javadoc) is just
package java.lang; public interface Cloneable { }
This means that Cloneable#clone()
does not exist (and trying to compile the method described above results in a compile-time error saying something like " cannot find symbol: method clone()
"). Should the Cloneable
source code have any effect on the effect of public Cloneable clone();
?
Why are we not allowed to assume that a class that implements Cloneable
has a public Cloneable clone()
method?
source share