Mar 15, 2012

Shallow Copy and Deep Copy in Java Cloning


Shallow and Deep Cloning



We create objects in Java by using the new operator. Every time the new operator is used, a fresh object is created with the default state as created by the constructors.

But if we want the state of new object to be same as of another object which has a different state than the new state then cloning is used.

I will quickly list some basic points about object cloning in Java as the focus of this tutorial is going to be on deep and shallow cloning:

1) The class whose objects are to be clones should implement Cloneable interface.

2) The clone() method is defines inside Object class and by default it throws CloneNotSupportedException.

3) One should override the clone() methof after implementing Cloneable interface.

Deep and Shallow cloning differ in the aspect of how the member reference variables behave in the original and clone.
If modifying the member reference variable by original object also modifies the member reference variable of clone object also then it is known as shallow cloning. But if the member reference variable is not shared among the original and clone object then it is known as deep cloning.

The code examples of deep and shallow cloning follow:

Shallow cloning example:

package com.example;

class Member{
private int a;

Member(int a){
this.a = a;
}

public int getA(){
return a;
}

public void setA(int a) {
this.a=a;
}
}

class Immutable {

private Member m;

Immutable(Immutable t){
this.m = t.m;
}

Immutable(int a){
m = new Member(a);
}

public int getMember(){
return m.getA();
}

private void setMember(int a){
this.m.setA(a);
}

public Immutable modifyM(int a) {
Immutable t1 = new Immutable(this);
t1.setMember(a);
return t1;
}
}

public class Test{

public static void main(String[] args) {
Immutable im1 = new Immutable(10);
System.out.println("Before Modify, im1 member is " + im1.getMember());
Immutable im2 = im1.modifyM(20);
System.out.println("After Modify, im1 member is " + im1.getMember());
System.out.println("After Modify, im2 member is " + im2.getMember());
}
}

Deep Cloning Example:

package com.example;

class Member{
private int a;

Member(int a){
this.a = a;
}

public int getA(){
return a;
}

public void setA(int a) {
this.a=a;
}
}
class Immutable {

private Member m;

Immutable(Immutable t){
this.m = new Member(t.getMember());
//this.m = t.m;
}

Immutable(int a){
m = new Member(a);
}

public int getMember(){
return m.getA();
}

private void setMember(int a){
this.m.setA(a);
}

public Immutable modifyM(int a) {
Immutable t1 = new Immutable(this);
t1.setMember(a);
return t1;
}
}

public class Test{

public static void main(String[] args) {
Immutable im1 = new Immutable(10);
System.out.println("im1 member is " + im1.getMember());
Immutable im2 = im1.modifyM(20);
System.out.println("im1 member is " + im1.getMember());
System.out.println("im2 member is " + im2.getMember());
}
}
Hope you liked this deep and shallow cloning example tutorial. If you have any question about the above code, then do leave a comment regarding the same.

No comments:

Post a Comment