• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deep Copies?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could you reform a shallow copy to a deep copy? For example, the field creator is the "Name" of the class type, the reference of the creator is c1 is copyied into c2. Thus c1.getCreator() == c2.getCreator() .
Any advice?
Thanks,
Ryan Vines
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps your question would be clearer if you were to post some more relevant example code.
Concerning the concept of a shallow copy versus a deep copy:
Consider the following (contrived) example.
The big difference between a shallow copy and a deep copy is that in the shallow copy, instance members are reused in the copy. In a deep copy, copies of instance members are used to create the copy.
Note that in the above example, any instance members of bar should also be deep-copied during the deep copy.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, well how would that to apply if you had subclasses and methods to deal with. Take this code for example
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We still have no idea what your question is.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, my question i am asking is how i could change a shallow copy to a deep copy. Like the exapmle i gave above creats shallow copies, not deep. I was wondering how you could change it to make the shallow copies deep copies.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To perform a deep copy, one would normally implement the Object.clone() method, implementing Cloneable .... much as in C++ with the copy constructor.
Inside the clone() method, we would call clone on all the instance variables, resulting in a separate but identical object, with no instance sharing between the two objects.
If, however, some of your instance variables don't implement Cloneable, you would have to manually clone them by using the objects exposed interfaces.
Maybe that would help you a bit. I didn't go through the code you posted. Much too long and way too lazy.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ryan vines:
ok, well how would that to apply if you had subclasses and methods to deal with. Take this code for example [...]


You would need to rework the clone() method like this:

Note that I removed the try..catch block so that CloneNotSupportedException will propagate to the caller. My take is that it's better to fail than return a null from the clone() method as it's expected that a real object will be returned.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Thanks alot guys, i fully understand how deep coping works now. and all that is thanks to you giving me help.
Sincerely,
Ryan Vines
 
reply
    Bookmark Topic Watch Topic
  • New Topic