Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between the deep copy and shallow copy in Object.clone()

 
Jasmine kaur
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
Please clear this concept of shallow copy and deep copy of the object when we invoke Object.clone();
Thanks in advance.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In cloneShallow, the returned object has a reference to the same object anObject. In cloneDeep, anObjecty is copied.

Object.clone() method performs a shallow copy, so when overriding this method, you should first call super.clone() and then modify some fields to perform the "deep" copy. (And returning a full indepedent object).
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to think of shallow/deep cloning in terms of parents and children. Shallow cloning copies the parent but shares the same children, whereas deeping cloning duplicates parents and children.

For example, if a parent A has 100 children objects (poor mother), then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

With deep cloning, when I created parent B all the 100 children would also be copied. So this would amount to 101 new objects and modifying parent A's children would not affect parent B's children. Think of the performance difference between creating one object versus 101, which is why deep cloning is definitely a more expensive call.

In most situations where you are even addressing cloning, I find you tend to want deep cloning, and have to implement it manually in all classes except where you use primitives (primitives are never shared, objects are always shared in shallow cloning). For example, if I have 1 parent class and multiple children and grandchildren classes, I'd have to write clone method in each and every class. Further, if I have arrays of children (very common) I have to clone the array by creating a new array, then manually cloning the children and adding them to the new array. One advantage, though, is that for cloning children you can delegate to their clone methods, so you never have to deal with more than 2 levels at a given time.
 
Hung Yee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI: Here are some more examples of 'deep copying' techniques from Sun:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
 
Jasmine kaur
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for responding , well Iam still confused so could u please make this statement clear what it means exactly
"if a parent A has 100 children objects (poor mother)"
then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

I would appreciate your suggestion if u make this statement clear as Iam still preplex.
Thanks in advance.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jasmine kaur:
Thanks alot for responding , well Iam still confused so could u please make this statement clear what it means exactly
"if a parent A has 100 children objects (poor mother)"
then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

I would appreciate your suggestion if u make this statement clear as Iam still preplex.
Thanks in advance.


I think that is Scott's attempt to add humor to his explanation. The "(poor mother)" is using a pun on the biological relationship beteween parents and children. It's also indicating that 100 children would be a very overwhelming responsibility for a single parent.

To understand the programming concepts, you should ignore the part in parenteses.

Layne
[ October 25, 2005: Message edited by: Layne Lund ]
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's better you have a or when someone is joking.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jasmine kaur:

"if a parent A has 100 children objects (poor mother)"


LOL, this is funny. There <i>is</i> an Indian legend in which a couple has 100 children.

 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is Scott's attempt to add humor to his explanation. The "(poor mother)" is using a pun on the biological relationship beteween parents and children. It's also indicating that 100 children would be a very overwhelming responsibility for a single parent.


I was trying to encourage mental visualation of the concept so it would be better remembered and understood! Or, I just have terrible sense of humor. Either way, my attempts at humor may doom us all!
[ October 28, 2005: Message edited by: Scott Selikoff ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic