Re: How is tag interface functionality implemented in Java ?
Lew wrote:
ankur wrote:
So u [sic] cannot call clone on an object of a user defined class
unless you
declare that the class implements cloneable interface.
Do you mean the Cloneable interface? Spelling counts.
I looked into the Object.java and Cloneable.java source files and did
not find any code connections between the two except the big comment
header before
You don't even need to read the source - the JLS and Javadocs will suffice.
protected native Object clone() throws CloneNotSupportedException;
that talks about Cloneable interface.
My question is how does Java JVM make sure that the an object cannot
call clone() method without implementing cloneable iterface ?
It doesn't. One can override clone() without implementing the Cloneable
interface. It's just normal inheritance.
To be more complete, the Object#clone() method is what enforces the
requirement, not the JVM itself. All the JVM does is run the code that
checks. If one follows the convention mentioned in the Javadocs for clone(),
then one ultimately does invoke Object#clone(), which does the check.
--
Lew