Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cloneable and deep copy.

 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a Cloneable object and its state consists only of primitaves, then calling super.clone() should return a deep copy, right?
For example:

Michael Morris
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily, if SomeClass is extending SomeOtherClass and it's not implementing Cloneable you will not get a deep copy.
In general clone() does a shallow copy. This article is a great source.
BTW, your signature is and unfortunately true.
Cheers,
Leslie
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[MM]: If I have a Cloneable object and its state consists only of primitaves, then calling super.clone() should return a deep copy, right?
If the state consists only of primitives, what's the difference between a shallow copy and a deep copy? I guess the answer is "yes" since there are no reference variables.
[LC]: Not necessarily, if SomeClass is extending SomeOtherClass and it's not implementing Cloneable you will not get a deep copy.
You mean, SomeClass extends SomeOtherClass and implements Cloneable, but SomeOtherClass does not implement Cloneable? It doesn't really matter whether SomeOtherClass implements Cloneable or not - only SomeClass needs to for this to work. Or were you making sure that SomeOtherClass had not already overridden clone() to provide a deep copy rather than shallow copy? It's true that unless some class somewhere in the inheritance hierarchy has overridden clone(), you get a shallow copy. (If there are any reference data fields to make this a relevant distinction.)
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the state consists only of primitives, what's the difference between a shallow copy and a deep copy?

None.
Michael Morris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic