aspose file tools*
The moose likes Beginning Java and the fly likes difference between cloneable and serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between cloneable and serializable" Watch "difference between cloneable and serializable" New topic
Author

difference between cloneable and serializable

kavitha vasu
Ranch Hand

Joined: Feb 22, 2007
Posts: 70
Hi Friends

What is the difference between cloneable and serializable in java?


Thanks
Kavitha
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
They aren't really related, so it's difficult to describe "the difference".

Both are marker interfaces (no operations), used to mark classes having special properties. However, the properties involved are largely unrelated.

Read the API for each interface.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Originally posted by Peter Chase:
They aren't really related, so it's difficult to describe "the difference".

Both are marker interfaces (no operations), used to mark classes having special properties. However, the properties involved are largely unrelated.

Read the API for each interface.


Well, I guess you could say they're related in the sense that you can implement the clone() method using object serialization, but that's about it, I guess.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Originally posted by Jelle Klap:


Well, I guess you could say they're related in the sense that you can implement the clone() method using object serialization, but that's about it, I guess.
No, you don't. You can use serialisation as a cheat's method to mimic cloning, but that isn't implementing the clone() method.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Um, I think you can implement clone() using the serialisation method.

Normally, when one implements clone(), one calls super.clone() to do a shallow copy, then does whatever extra stuff is required. But for the serialisation method, you don't call super.clone() at all; you do all the copying yourself.

I don't recommend this approach. In fact, I don't recommend using clone() at all, if you can avoid it. But I think it is feasible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
I know you will call me pedantic, but you aren't using serialisation to implement clone(). You can use it to circumvent clone(). As you say, people rarely use clone() anyway.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Tangential aside: I totally heard Laurie Anderson asking "Que es mas macho, pineapple o knife?"
[ April 25, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Originally posted by Campbell Ritchie:
I know you will call me pedantic, but you aren't using serialisation to implement clone(). You can use it to circumvent clone(). As you say, people rarely use clone() anyway.


I think you can do either, really. It's probably more common to use this technique to circumvent clone(), as you say. But you could also use this technique to provide a clone() implementation that gives a deep copy, without having to detail all the fields yourself. This might make sense for a base class that will be extended by many others, where you want deep copies and you don't want to have to write all those clone() methods individually. And you don't care if your clone() method is notably slower than other common implementations would be. (Like most other performance issues, most of the time it doesn't really matter anyway.) I agree that clone() isn't used that often anyway, and this usually wouldn't be a good way to implement it. But sometimes it might make sense.


"I'm not back." - Bill Harding, Twister
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
what's the difference?

serialization is on the exam, and cloneable isn't


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
I take your point, Jim, but Bert's was a better point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between cloneable and serializable