Nov
30
2014

Deep Copy And Shallow Copy in java

Hi in my last post object class  in java and method of object class we discussed about clone method of object class and what is object cloning .I also discussed about shallow copy and deep copy .In this post we i will discuss succinctly what is difference between Deep Copy And Shallow Copy and  will show implementation of shallow copy and deep copy with program and image.

What is object cloning

Object cloning is the process of making copy of an object.To make copy of an object we use protected Object clone() throws CloneNotSupportedException of object class.There are two ways of creating copy of object .1 Shallow Copy  2 Depp copy.

what is shallow copy

Basically  in shallow copy if content of original or cloned object is modified then it will reflect in other .For example if i made any change in original object then it will also reflect in cloned object .And if i made any changes in cloned object then it will also reflect in Original object.

Now to get deeper in shallow copy.What happen in shallow copy so that when changes made in one object reflect in other.

In shallow copy both object(i.e Original object and Cloned object) have same reference to update .So whenever any changes made they reflect back in other also. Have a look in below image.

shallow copy  image

In above image both Original object and cloned object have same reference of address .So when we made changes in any one it will reflect in other also .But both object have different copy of name field. So any change made in name field would not get reflect in other object.Now let see how to implement Shallow copy .

how to implement Shallow copy


Here in below program we have  Address class with two fields houseNo and street, Person class which have a reference of Address class as one of its field and name as other field.Employee class with main method and for creating object of Person class and to create copy of this object.

Here is output of above program.

shallow copy

As you can see After making changes the difference is that address object field value get change because clone object and original object hold same reference of address object.But Original object and Clone object both have different copy of name field .So even after update name field in original object it  does’t get change in cloned object.

what is deep copy

In deep copy if changes made in original object after creating copy of it then these changes would’t get reflect in copy .This is because original object and cloned object does’t share any reference .Have a look in below image to understand easily.Deep copy image1As you can see original object have different reference of address object and cloned object have different copy of address.So after making change in original object changes does’t appear in cloned object.Now let see how to implement Deep copy  using code.

 How to implement Deep copy


Address class and Employee class is same .There is change in Person class clone() method .

Here is output of above program.

Deep copy

 

You can see after making changes in original object value of cloned object fields remain same .This is because there is no shared reference between these object.

This is all about anatomy of Shallow copy and deep copy .Hope you have enjoyed it .I will get back very soon with next post by then enjoy ,have lots of fun  and take care.

 

About the Author: devender kumar

java/j2EE developer

Comments are closed.