Tuesday 6 September 2011

clone() method of Object class

 clone() is method of  Object Class. It is overridden in class that implements Cloneable interface. 
It is used to make a copy of object and set properties of object as required so that we do not have to create new object each time we need and set its properties.

Class that contain only primitive instance can be cloned easily by implementing Cloneable interface and overriding clone() method. But if Class also contains instance of another Class, than that Class should also implement an Cloneable interface and override clone() method. Otherwise Copy of object will not be created and exception will be thrown CloneNotSupportedException and no new Object of that class will be created .
Program 1 :

class Person implements Cloneable {
private Car car;
private String name;
private Integer no;

public Car getCar() {
return car;
}
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Person(Integer n, String s, String t) {
no = n;
name = s;
car = new Car(t);
}
public Object clone() {
// shallow copy
try {
return super.clone();
} catch (CloneNotSupportedException e) {//exception will be thrown so null will be returned & no           //object      will     be created
return null;
}
}
public void setNo(Integer no) {
this.no = no;
}
public Integer getNo() {
return no;
}
}

class Car {

private String name;

public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Car(String s) {
name = s;
}
}

public class ShallowCopyTest {

public static void main(String[] args) {
// Original Object
Person p = new Person(new Integer(1000), "sreeRama", "Garuda");
System.out.println("Original (orginal values): " + p.getNo() + " "
+ p.getName() + " - " + p.getCar().getName());
// Clone as a shallow copy
Person q = (Person) p.clone();
System.out.println("Clone (before change): " + q.getNo() + " "
+ q.getName() + " - " + q.getCar().getName());
// change the primitive member
q.setName("Indra");
q.setNo(new Integer(2000));
// change the lower-level object
q.getCar().setName("Iravath");
System.out.println("Clone (after change): " + q.getNo() + " "
+ q.getName() + " - " + q.getCar().getName());
System.out.println("Original (after clone is modified): " + p.getNo()
+ " " + p.getName() + " - " + p.getCar().getName());
}
}
//output :
Original (orginal values): 1000 sreeRama - Garuda
Clone (before change): 1000 sreeRama - Garuda
Clone (after change): 2000 Indra - Iravath
Original (after clone is modified): 1000 sreeRama - Iravath

Program 2
class Person implements Cloneable {
// Lower-level object
private Car car;//it implements cloneable and override clone()
private String name;

public Car getCar() {
return car;
}
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Person(String s, String t) {
name = s;
car = new Car(t);
}
public Object clone() throws CloneNotSupportedException{
// Deep copy
Person p = (Person) super.clone();
p.car = (Car) p.car.clone();
return p;
}
}
class Car implements Cloneable{

private String name;

public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Car(String s) {
name = s;
}
@Override
protected Object clone() throws CloneNotSupportedException {//everything is perfect
return super.clone();
}
}
public class DeepCopyTest {

public static void main(String[] args) {
// Original Object - before Deep cloning
Person p = new Person("sreeRama", "Garuda");
System.out.println(p.getName()+" "+p.getCar().getName());
Person pClone = null;
try {
pClone = (Person) p.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("Clone (before change): "+pClone.getName()+" "+pClone.getCar().getName());
pClone.setName("Indra");
pClone.getCar().setName("Iravath");
System.out.println("Clone (after change): "+pClone.getName()+" "+pClone.getCar().getName());
// Original Object - after Deep cloning
System.out.println(p.getName()+" "+p.getCar().getName());
}
}
// Output :
sreeRama Garuda
Clone (before change): sreeRama Garuda
Clone (after change): Indra Iravath

No comments:

Post a Comment