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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Purpose of Cloneable interface" Watch "Purpose of Cloneable interface" New topic
Author

Purpose of Cloneable interface

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi everybody, I have a doubt regarding Cloneable interface, why should I implement it, when I was reading about it, the author said if you want the exact copy of an object we can use object.clone().

When will I come across such a situation, to make an exact copy of an object?

Please can anyone explain me?

Thank you all in advance.


Love all, trust a few, do wrong to none.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Have you read the API for java.lang.Cloneable? It has two "See Also" links that should answer your questions in totality.


luck, db
There are no new questions, but there may be new answers.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

I know the purpose of Cloneable, but where in programming I will come across its usage?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
By "usage" i presume you mean "use".

When you have a mutable object which you wish to pass a copy of, so the copy can be manipulated without altering the original.
siddhu Math
Ranch Hand

Joined: Aug 10, 2006
Posts: 46
one of the secineros that I may recollect from my experience is
when using calender Api of java.... during the start and end date computation which we did on the cloned object of the calender instance....
and even on the clent VO objects(unable to recollect the correct context) when we need to modify any values of the client on the server so we do it using the clone object .....not the original object...
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you all, can anyone please tell me how to clone my own object, suppose I have a class A and want to clone A's object. Is it possible?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18658
    
    8

I would have thought that the two links which Darryl Burke referred to would have answered that question too. If they didn't, it would help if you asked a specific question about your problem. "Write me a tutorial" isn't a polite thing to ask when you've already been referred to a couple of them.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Hello Chaitanya,

Clonable interface is basically a tagging interface, which means that it just ensures that you are implementing some required method (in this case this method is clone).

If you want to make your class clonable and prepare a cloned object then you need to do things:
1. Implement clonable interface for the class.
2. Override Clone() method provided by Object class.



Output:
@@@@@@@@@@@@@@@@@@@@ SuperClone @@@@@@@@@@@@@@@@@@@
I am real
I am cloned


So, here line no. 12 is actually doing cloning of your object.
If you had not implemented the clonable interface, you would have got ClassCastException.

So, this is the use of this interface and that is how you can clone your object.

Thanks.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

himanshu.harish agrawal wrote:If you had not implemented the clonable interface, you would have got ClassCastException.

Nope. Read the API.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Darryl Burke wrote:
himanshu.harish agrawal wrote:If you had not implemented the clonable interface, you would have got ClassCastException.

Nope. Read the API.


Sorry, my mistake, it is CloneNotSupportedException. I have just mis-written ClassCastException.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you guys for explaining clearly.
 
 
subject: Purpose of Cloneable interface