File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between cloneable and serializable

 
kavitha vasu
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends

What is the difference between cloneable and serializable in java?


Thanks
Kavitha
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tangential aside: I totally heard Laurie Anderson asking "Que es mas macho, pineapple o knife?"
[ April 25, 2008: Message edited by: Bill Shirley ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the difference?

serialization is on the exam, and cloneable isn't
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take your point, Jim, but Bert's was a better point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic