aspose file tools*
The moose likes Java in General and the fly likes Cloneable and deep copy. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloneable and deep copy." Watch "Cloneable and deep copy." New topic
Author

Cloneable and deep copy.

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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


Normal is in the eye of the beholder
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.)


"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

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

None.
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloneable and deep copy.