• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

difference between cloneable and serializable

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends

What is the difference between cloneable and serializable in java?


Thanks
Kavitha
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's the difference?

serialization is on the exam, and cloneable isn't
 
Campbell Ritchie
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point, Jim, but Bert's was a better point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic