I am trying with cloning function capabilities.
I have found strange thing with it.
Though my class implements cloneable interface, its object
cannot be cloned in some other class, though CloneNotSupported
exception catching facility provided in that other class.
I get a compilation error for this, saying class in which object
is getting cloned is not a super class, for object being cloned.
To my surprise,
when I extend the class with the one, in which I am going to clone this
class it works without hassles.
Doubt is, since clone method is protected in Object class, why it cannot be directly used ?
For better clarification I am providing code alongwith.
The code which works,
public class CloneTest
{
public static void main(
String args[]) throws CloneNotSupportedException
{
CloneTest ct = new CloneTest();
ct.cloneFunction();
}
public void cloneFunction() throws CloneNotSupportedException
{
CloneableObject coOriginal = new CloneableObject();
CloneableObject coCloned = (CloneableObject)coOriginal.clone();
System.out.println("Original Object Value "+coOriginal.getI()+" & Clone Reference "+coOriginal.toString());
System.out.println("Cloned Object Value "+coCloned.getI()+" & Clone Reference "+coCloned.toString());
}
}
class CloneableObject extends CloneTest implements Cloneable
{
private int i=10;
public int getI()
{
return i;
}
}
I am not getting, why CloneableObject has to extend with CloneTest class ?