Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How Serializable Interface Work?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one please tell me how serializable interface works even that has no methods(marker interface).

Thanks

Shashi Bhushan
SCJP,SCJD
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shashi --

The Serializable interface is what's called a tagging interface. If a class MyClass implements a tagging interface like Serializable, then other code can say, in effect,



That's the whole reason for its existence; the JVM decides whether serializing a class is legal or not based on whether it implements this interface.
[ May 31, 2004: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume Ernest actually meant this:



The Serializable interface demands adherance to a specific contract. When a class implements the interface, it is done so with the understanding that the class understands and adheres to the contract, thus it is called a "tagging interface" - meaning "This class is tagged as understanding what it means to be Serializable".

A good real world example is hiring a lawyer. You (the class) understand that you are engaging in a contract by hiring a lawyer (the interface). That contract involves giving the lawyer money (the Serializable contract) but the lawyer doesn't have to actually do anything for that money (the Serializable interface contains no methods), and at the end of the day, nothing will have been achieved by you (the class), as long as you adhere to contract - the lawyer gets paid.
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
A good real world example is hiring a lawyer. You (the class) understand that you are engaging in a contract by hiring a lawyer (the interface). That contract involves giving the lawyer money (the Serializable contract) but the lawyer doesn't have to actually do anything for that money (the Serializable interface contains no methods), and at the end of the day, nothing will have been achieved by you (the class), as long as you adhere to contract - the lawyer gets paid.



well, that clears things up...
:roll:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect it's actually a pretty good analogy, if you've ever had to hire a lawyer. But I can see where it may not be clear to everyone. Alternately:

Java provides a pretty good implementation of serialization, by default. Basically it just goes through all the fields of a class (and its superclasses) and writes them to a stream. OK, cool, this is all you need for many classes. It's a pretty good default. However, it's not always appropriate. And example is the Connection class. You can write the parameters of a connection to a file somewhere, fine. However if you deserialize it - you read the host name and use name and password and whatver else is necessary, and create a Connection using those parameters - have you really recreated the original Connection? Well, no, at least not as far as the DB is concerned. You've merely created a new Connection with the same parameters as the original. If, for example, you close() the original Connection, this would have no effect on the new one. So serialization isn't really appropitate here, at leat not in the sense of reconstituting the original object. We don't want people to think they've rebuilt the original object here - that would be too confusting. There are other classes it may not be appropriate to serialize. Java chose to make the default serialization process an opt-in mechanism - that is, Java will only serialize a class if the class declaration specifies that the default serialization mechanism is appropriate. (Or they can specify an alternate mechanism via Externalizable, but that's another post.) The way to opt in is simple - just specify that the class implements Serializable. That's it. The JVM can test this easily enough - the default implementation of writeObject() can be thought of as:

In other words, the only role of a marker interface like Serializable is as a sort of binary flag. Either the JVM should use the default serialization process, or it shouldn't. It's up to the class author to decide whether (obj instanceof Serializable) should return true - if so, you'd better implement Serializable.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Learn How Serialization works Internally with Java, This is called Object Serialization Protocol

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic