Java Cloning
1) What is Cloning? What importance does it have?
Ans: Creating a copy of object is known as cloning. It has special importance in Java because object cloning is supported by Java itself. Though one can override the behavior of object cloning but there is a default behavior by which copies of objects can be created.
2)How does one create object clones?
Ans: One can create object clones by invoking the clone method on the object to be cloned. The clone() method is defined as protected method in the Object class and is thus inherited by all the classes.
The only requirement for creating object clones is that the class whose objects are being cloned should implement the Cloneable interface. If this is not done that a CloneNotSupported Exception is thrown on calling the clone method.
3) What is the different ways to create object clones?
Ans: Cloning is of two types viz shallow and deep cloning. Shallow cloning is what the default behavior of clone method is and deep cloning is achieved by overriding the clone method in a class.
With shallow cloning the member primitive variables are also cloned but the member reference variables are not cloned. This means that the reference variables referring to objects are shared between the original and cloned objects.
4) Give an example code for both shallow and deep cloning?
Shallow Cloning:
public class Test implements Cloneable{
int a=10;
StringBuffer str = new StringBuffer("abc");
public void main (String args[]) throws CloneNotSupportedException{
Test t1 = new Test();
Test t2 = (Test)t1.clone();
t1.a=20;t2.a=30;
t1.str.append("def");t2.str.append("ghi");
System.out.println("t1.a = " + t1.a + " t2.a = " + t2.a);
System.out.println("t1.str = " + t1.str + " t2.str = " + t2.str);
}
}
Output:
t1.a = 20 t2.a = 30
t1.str = abcdefghi t2.str = abcdefghi
Deep Cloning:
public class Test implements Cloneable{
int a=10;
StringBuffer str = new StringBuffer("abc");
public void main (String args[]) throws CloneNotSupportedException{
Test t1 = new Test();
Test t2 = (Test)t1.clone();
t1.a=20;t2.a=30;
t1.str.append("def");t2.str.append("ghi");
System.out.println("t1.a = " + t1.a + " t2.a = " + t2.a);
System.out.println("t1.str = " + t1.str + " t2.str = " + t2.str);
}
@Override
protected Object clone() throws CloneNotSupoortedException{
Test t = new Test();
t.str = new StringBuffer();
return t;
}
}
Output:
t1.a = 20 t2.a = 30t1.str = abcdef t2.str = ghi
Ans: Creating a copy of object is known as cloning. It has special importance in Java because object cloning is supported by Java itself. Though one can override the behavior of object cloning but there is a default behavior by which copies of objects can be created.
2)How does one create object clones?
Ans: One can create object clones by invoking the clone method on the object to be cloned. The clone() method is defined as protected method in the Object class and is thus inherited by all the classes.
The only requirement for creating object clones is that the class whose objects are being cloned should implement the Cloneable interface. If this is not done that a CloneNotSupported Exception is thrown on calling the clone method.
3) What is the different ways to create object clones?
Ans: Cloning is of two types viz shallow and deep cloning. Shallow cloning is what the default behavior of clone method is and deep cloning is achieved by overriding the clone method in a class.
With shallow cloning the member primitive variables are also cloned but the member reference variables are not cloned. This means that the reference variables referring to objects are shared between the original and cloned objects.
4) Give an example code for both shallow and deep cloning?
Shallow Cloning:
public class Test implements Cloneable{
int a=10;
StringBuffer str = new StringBuffer("abc");
public void main (String args[]) throws CloneNotSupportedException{
Test t1 = new Test();
Test t2 = (Test)t1.clone();
t1.a=20;t2.a=30;
t1.str.append("def");t2.str.append("ghi");
System.out.println("t1.a = " + t1.a + " t2.a = " + t2.a);
System.out.println("t1.str = " + t1.str + " t2.str = " + t2.str);
}
}
Output:
t1.a = 20 t2.a = 30
t1.str = abcdefghi t2.str = abcdefghi
Deep Cloning:
public class Test implements Cloneable{
int a=10;
StringBuffer str = new StringBuffer("abc");
public void main (String args[]) throws CloneNotSupportedException{
Test t1 = new Test();
Test t2 = (Test)t1.clone();
t1.a=20;t2.a=30;
t1.str.append("def");t2.str.append("ghi");
System.out.println("t1.a = " + t1.a + " t2.a = " + t2.a);
System.out.println("t1.str = " + t1.str + " t2.str = " + t2.str);
}
@Override
protected Object clone() throws CloneNotSupoortedException{
Test t = new Test();
t.str = new StringBuffer();
return t;
}
}
Output:
t1.a = 20 t2.a = 30t1.str = abcdef t2.str = ghi