Cloning: Cloning means creating the new object with the same state as the object you are cloning.
Default Cloning in Java: Shallow Cloning
I will explain Shallow Cloning with an example.
Suppose if I have StudentShallowEx class with following members
a)sid(1)
b)sname(String)("Anil")
c)address (This is an instance of class Address)
Address class has following members:
a)city(String)
When you clone the StudentShalloEx object(one in the following code) by calling clone() method,
new StudentShalloEx will be created with the state of the original object you are trying to clone.
The drawback of this... is referenes will be copied to cloned object.So if you change the state
of the address object in cloned object,original object will be affected.
1)sid same as in original object i.e 1
2)and sname as "Anil"
3)and the address reference in original object is copied to address reference in cloned object,so the
address reference in original object and cloned object points to the same Address object thereby
any changes you make to address reference in cloned will affect the original object.....
So,if you modify the address reference in cloned object it will change the address in
the original object.
But even if you change the sid in cloned object it won't affect the sid in the original object.
Imp Point: So if your object has only primitives and immutable objects like String,shallow
copy will be sufficient.
But if you have other class objects which are mutable like address in our example you have
to create deep copy.... so that changes you make to cloned object will not affect the original
object...
ShallowCloning Example Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class Address {
private String city;
Address(){
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class StudentShallowEx implements Cloneable{
private int sid;
private String sname;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public StudentShallowEx() {
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public static void main(String args[])throws CloneNotSupportedException{
StudentShallowEx one = new StudentShallowEx();
one.sid=1;
one.sname="Anil";
Address add1 = new Address();
add1.setCity("HYD");
one.setAddress(add1);
System.out.println(one.sid);
System.out.println(one.getAddress().getCity());
StudentShallowEx two = (StudentShallowEx)one.clone();
two.getAddress().setCity("HYD is modified because it is shallow copying");
two.setSid(2);
System.out.println(one.sid);
System.out.println(one.getAddress().getCity());
}
}
DeepCloning:
When you do the deep clone of the Student,when you change the address in cloned object
it won't affect the address state in the original object.
In shallow cloning if you change the state of the address in cloned object,it will change
the address state in original object
To do deep cloning you have to do the following changes
--------------------------------------------------------------------------
1)Address class should implement Cloneable
2)It should override the clone method
3)StudentDeepCopy should override the clone method.
Deep Cloning code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class Address implements Cloneable{
private String city;
Address(){
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class StudentDeepEx implements Cloneable{
private int sid;
private String sname;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public StudentDeepEx() {
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
protected Object clone() throws CloneNotSupportedException {
StudentDeepEx cloneOb = (StudentDeepEx)super.clone();
cloneOb.address = (Address)cloneOb.getAddress().clone();
return cloneOb;
}
public static void main(String args[])throws CloneNotSupportedException{
StudentDeepEx one = new StudentDeepEx();
one.sid=1;
one.sname="Anil";
Address add1 = new Address();
add1.setCity("HYD");
one.setAddress(add1);
System.out.println(one.sid);
System.out.println(one.getAddress().getCity());
StudentDeepEx two = (StudentDeepEx)one.clone();
two.getAddress().setCity("HYD is modified because it is shallow copying");
two.setSid(2);
System.out.println(one.sid);
System.out.println(one.getAddress().getCity());
}
}
No comments:
Post a Comment