• 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

clarification on cloning

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


although the above code is pretty blank, can I clone the states of the super class this way? its not printing the stack trace of the exception. Does that mean that its silently not cloning the states of the super class?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it means it's cloning the whole object without error, including any fields from the superclass Test. You could verify this by adding a field to Test and verifying that its data is present in the clone.
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but this means that you could create a clone of the super class even if the author of the super class didn't want you to create one? cause i read somewhere that if you want to prevent someone from creating a clone, simply do not implement Cloneable interface. but then is this is a loop-around which makes the whole process ineffective. what am i missing here?
[ November 24, 2005: Message edited by: Rahul Kakkar ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. I wouldn't say that this "makes the whole process ineffective" - that's an exaggeration. Though the cloning process has a number of additional quirks which make it less than ideal, but that's another discussion. In general I would say that this particular problem with Cloneable doesn't cause much trouble usually. If you want to guarantee that no subclass of your (non-Cloneable) class can be cloned, you have two options: make the class final, or override clone() with a final method that prevents actual cloning from occurring. I.e.:

[ November 25, 2005: Message edited by: Jim Yingst ]
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it. thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic