• Post Reply Bookmark Topic Watch Topic
  • New Topic

Purpose of Cloneable interface  RSS feed

 
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API for java.lang.Cloneable? It has two "See Also" links that should answer your questions in totality.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the purpose of Cloneable, but where in programming I will come across its usage?
 
Marshal
Posts: 59146
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 23512
47
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for explaining clearly.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!