Thursday, March 14, 2013

Shallow Cloning and Deep Cloning in Java

Cloning is of 2 Types:

1. Shallow Cloning  2. Deep Cloning

Whenever clone() method is used 2 different objects are created in 2 different memory locations. So
separate memory is occupied.

If a class contains  X, Y variable and clone is applied two different objects are created in two different situations.

In Shallow Cloning  main problem is instance variables are common for both objects , so if one object is modifed then automatically second object is also modified.

In deep cloning 2 different objects are created in 2 different memory locations , if in case original object is modified then also its duplicate object won't effect , why because there is  no relation between original and duplicate object.



No comments:

Post a Comment