Steve Luke wrote:Since you know (as a programmer) that the T implements Cloneable, and therefore has a public clone()
No you don't. The chances are high that clone() will be public, but it's perfectly legal to implement Cloneable and keep clone() protected, or not even overwrite it at all.
Also, the contract for clone() does not require it to return an object of the same class. The returned object can even be unequal to the source. It is in fact very, very loose in what a programmer should do when overriding it. Of course most programmers use super.clone() in the entire chain, but it's not required to do so.
Yaroslav Chinskiy wrote:I guess who ever designed clone() did not intend it to be used outside of inheritance. sad.... :banghead:
A lot of people think the bigger problem is in Cloneable - they think it should enforce clone() to become public. I don't know; yes it would be useful because you could check for Cloneable, cast to it where necessary and simply call clone(). But that does force you to make clone() public, while you may want to keep it protected (don't ask me why).