• 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

Cloneable vs. copy constructor

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any advantage to implementing the Cloneable interface as opposed to defining a "copy constructor"


Is there any difference between the instances constructed at *1 and *2?
[ March 06, 2006: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, since no one commented on this topic. This is an article I found that describes an interview with Josh Bloch on the subject. It seem he is an advocate of not implementing Clonable at all, but of defining copy constructor to handle the task. I welcome anyone else's opinion on the topic.
[ March 08, 2006: Message edited by: Garrett Rowe ]
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cloning does a shallow cloning.

For example, if you clone a HasMap only the Map will be cloned and objects stored in the HashMap will still be pointing to the same object.

So, it is better to provide your own copy method where you can deep clone , i.e. cloning the object inside HasMap as well.

One easy way to code this is to serialize and deserialize the object. Having said that serialization adversely affect performance. If performance is a concern then write your own copy method.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I prefer copy constructors. I've never liked Cloneable and I still don't. Bloch put it in much more eloquent terms than I could ever hope to so I'll leave it at that.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ak pillai:
Cloning does a shallow cloning.

For example, if you clone a HasMap only the Map will be cloned and objects stored in the HashMap will still be pointing to the same object.



Map's clone is defined in that way because, given a shallow copying method, you can always build on it to define a "deep copy", whilst defining a shallow copy using a deep copy method is much harder

When overriding clone on your own classes, you are free to define it to do a deep copy.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I miss with *just* a copy constructor is polymorphic copying. Suppose you have a Container of Components (where Component at least has various subclasses and is probably abstract, too). How do you make a deep copy of a container? Component should have a method like:

public Component deepCopy()

If WidgetComponent implements this using a copy contructor, that's ok:

public Component deepCopy() {return new WidgetComponent(this);}

But stopping at just a copy contructor isn't always enough.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic