aspose file tools*
The moose likes Java in General and the fly likes clone method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "clone method" Watch "clone method" New topic
Author

clone method

Poornachandran R
Ranch Hand

Joined: Sep 11, 2002
Posts: 47
Why clone method is given in Object class. It can be easily declared in Clonnable right ?
Poorna
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jan 30, 2002
Posts: 3451
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Vlad Roubtsov
Greenhorn

Joined: Jun 14, 2003
Posts: 9
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.


<a href="http://emma.sourceforge.net" target="_blank" rel="nofollow">EMMA</a>: an open-source code coverage toolkit
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
 
subject: clone method