• 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

Stop the cloning of an object.

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

I was recently asked in an interview that how can i avoid cloning of an object of an predefined class like String... can anyone please help me out
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if a class can be subclassed, you can always override the clone method and have it throw the CloneNotSupportedException or any RuntimeException.

String class cannot be subclassed, so I'm not sure how you can change that behavior in the case of String class. Doesn't look like a possible thing to me. That would mean <String>s could behave differently. That is not what the design of the String class allows.

Also I don't understand why would anybody want to prevent cloning of a subclass if the super class allows it. Because a subclass is-a super class and being cloneable is one behavior of super class that should be passed on to the subclass. Would you then want to reconsider your inheritance scheme. You could always replace what is now a subclass with a non subclass and have it contain a what is now a super class instance?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another "well thought" interview question! In my opinion, the correct answer to that question is "don't call the clone() method". This definitely avoids any cloning.

If the question was really meant to be "how to prevent cloning of a class I have no control over" (as I guess it was), then several things are wrong with the question. Most of all, String is a particularly unsuitable example: firstly, String doesn't support cloning at all (it doesn't implement Cloneable), so the goal is already achieved. Secondly, String is immutable, and therefore, even if it could be cloned, no one would ever do it - you don't need several independent copies of an immutable object. Thirdly, even if String could be cloned, no harm could be done by the cloning, so there isn't any reason whatsoever to prevent someone from cloning a String.

In any case, I'm not aware of any reasonable way to prevent a class that correctly implements Cloneable interface from being cloned.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

firstly, String doesn't support cloning at all (it doesn't implement Cloneable)



Thanks, Martin. I should have checked. :-)

And I like your response -- "don't call the clone() method". Nice. I will say exactly this if someone asks me this question.


If the question was really meant to be "how to prevent cloning of a class I have no control over" (as I guess it was), then several things are wrong with the question. Most of all, String is a particularly unsuitable example: firstly, String doesn't support cloning at all (it doesn't implement Cloneable), so the goal is already achieved. Secondly, String is immutable, and therefore, even if it could be cloned, no one would ever do it - you don't need several independent copies of an immutable object. Thirdly, even if String could be cloned, no harm could be done by the cloning, so there isn't any reason whatsoever to prevent someone from cloning a String.



And the above are important things to see. Thank you.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:Thanks, Martin. I should have checked. :-)


I didn't mean my post as an correction of yours. Actually, I haven't seen your response before posting mine, because I opened the page in the morning and get to it later.

I got irked by the "interview" factor. If you read some really misleading and imprecise question on the Ranch, chances are it came from an interview. It shows that interviews are often lead by people with nearly zero knowledge on the subject.
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I didn't mean my post as an correction of yours.



I hope that doesn't imply that if I'd do a mistake in future, you wouldn't correct me. :-) I loved the correction, regardless. Particularly - 'don't call clone', was hilarious ( though yes, in all fairness, it's the only correct answer in my opinion as well ).

I like it when you ( or anybody here ) correct ( corrects ) us if we are thinking in the wrong direction, or if we are biased towards one approach of solving a problem statement, or if we haven't considered some important aspect. So though you didn't mean to correct me this time, I'd say, please correct me every time you feel I'm wrong. I will hopefully then not make that mistake where it would really count.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic