Shallow Copy - All inheritance tree objects are not cloned just references are cloned .It still refer to old objects in hierarchy level .Changes in old mutable objects will reflect .You might need to create clone of member objects and set in clone method.In that case all object tree must implement clonable marker interface .Note that clone method does not invoke constructor or initialization blocks of class .It is just bianry copy of object .
Deep Copy (Serialization )- All inheritance tree are cloned /copied .Changes in mutable object state are not reflected as objects are different . All object tree must implement "serializable" marker interface .
We have to take care of transient variables ourselves .
Need of serialization - To get object state in binary format and pass it over wire as 1's and 0's .So that it can be loaded in class loader of another JVM .
package salpejava.clone;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TestClone {
public static void main(String[] args) {
Person person = new Person();
Address address = new Address();
address.setAdress("OLD ADDRESS");
person.setId(10);
person.setAddress(address);
try {
Person shallowPerson = (Person) person.clone();
System.out.println(shallowPerson.toString());
address.setAdress("NEW ADDRESS");
System.out.println(shallowPerson.toString());
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Person person2 = new Person();
Address address2 = new Address();
address2.setAdress("OLD ADDRESS 2 ");
person2.setId(20);
person2.setAddress(address2);
Person clonedPerson2 = person2.getDeepCopy();
System.out.println(clonedPerson2.toString());
address.setAdress("NEW ADDRESS 2");
System.out.println(clonedPerson2.toString());
}
}
class Person implements Cloneable,Serializable {
/**
*
*/
private static final long serialVersionUID = -1605117478578295247L;
private int id = 10;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Person [id=" + id + ", address=" + address + "]";
}
public Person getDeepCopy () {
Person deepPerson = null;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream outputStream = new ObjectOutputStream(arrayOutputStream);
outputStream.writeObject(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
ObjectInputStream inputStream;
try {
inputStream = new ObjectInputStream(arrayInputStream);
deepPerson = (Person)inputStream.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return deepPerson;
}
}
class Address implements Cloneable,Serializable {
/**
*
*/
private static final long serialVersionUID = -4056104942549317741L;
private String adress = "";
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
@Override
public String toString() {
return "Address [adress=" + adress + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
No comments:
Post a Comment