File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone method

 
Poornachandran R
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why clone method is given in Object class. It can be easily declared in Clonnable right ?
Poorna
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method is provided because the Object class already provides you with a native implementation that is more efficient than anything you could ever do yourself. So all you need to do is indicate that you're happy for your objects to be cloned -- by implementating Cloneable -- and usually also by widening the visibility of the clone() method to public.
- Peter
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Poornachandran,
Welcome to JavaRanch, the absolute best site on the www for Java information. We don't have many rules around here, but we do have one. Please change your display name to a first and last name to comply with the JavaRanch Naming Policy. You can change it here. Thank you for your cooperation.
Adding to what Peter said, Cloneable is like Serializable. They are both tagging interfaces. By implementing them, as Peter says, you give your permission for your objects to be cloned or serialized. One other thing to consider about Object's implementation of clone() is that it provides a shallow copy. That may or may not be a problem. If the cloned object's state consists of only primatives and immutables then you have nothing to worry about. However, if the cloned object contains references to other mutable objects, then you will probably want to override clone() and create a deep copy of the object, unless you don't mind having several objects changing the state of the mutables.
 
Vlad Roubtsov
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
The method is provided because the Object class already provides you with a native implementation that is more efficient than anything you could ever do yourself. So all you need to do is indicate that you're happy for your objects to be cloned -- by implementating Cloneable -- and usually also by widening the visibility of the clone() method to public.
- Peter

The face that Object.clone() is protected and is not a part of Cloneable [which would have forced it to be public] is widely recognized as a big mistake.
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The face that Object.clone() is protected and is not a part of Cloneable [which would have forced it to be public] is widely recognized as a big mistake.
By whom? Can you provide a more efficient shallow copy than Object.clone()? I suppose that one benefit would be the elimination of CloneNotSupportedException but past that I can't see why it would be better to put clone() in the Cloneable interface.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic