Serialization marker Interface

Grssridhar Ramakrishnan

Greenhorn

Posts: 1

posted 4 years ago

Hi there !! Im preparing for OCJP . In Which I came up with a doubt !!! According to the book , Serialization has no methods to implement !! I got a doubt here . The interface is not even implementing the method then what is the use of using the serializable Interface . I m thinking then for what purpose we are implementing the specially serializable interface.. Anyone give me the explaination about the marked interface and normal interface ?this quest may be silly but i dont think so rather i can ask here to get clear in JAVA

sahil kumar garg sangrur

Greenhorn

Posts: 19

posted 4 years ago

this is a marker interface in java to check that if the particular class is implementing this interface only then allowed to write the object of particular class in stream..otherwise not allowed

Ashok Donepudi

Greenhorn

Posts: 2

posted 4 years ago

Hi Ramakrishnan,

When a class implements serializable it informs the JVM that this class needs to be serializied. when a class is marked serializable it generate a serialUID for this class and compares when you desirialize the object.

As already stated, a marker interface is simply an interface with no methods.

Their value is an oft-debated subject, since it would seem to go against the grain of Object-Orientation; but I'd say that they have two possible advantages:

1. They serve the purpose of a boolean field without the added space.

2. They allow you to document an aspect of behaviour which might not otherwise be obvious. A good example of this is the RandomAccess marker interface.

All I can tell you is: they're rare. And one of the most fiercely debated ones (java.lang.Cloneable) should probably never have been a marker interface to begin with.

this quest may be silly but i dont think so...

There are no silly questions. I can't vouch for the answers though.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here

Vignesh Gopal

Greenhorn

Posts: 16

posted 4 years ago

It can also be called as Tagged Interface, Dummy Interface, Empty Interface.

We are implementing this Interface just for tagging purpose.

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

We are using this interface to backup the current state of object and reusing it later through deserialization. If we want to send a value to one place to another usually we will save the data in DB and then transfer it. Instead we are send the data through network as a Object and in another end we will deserialize it.

Another realtime example for this is, When we saving a particular game in PC a file will be saved. That file will save the current instances of the object. Later it will deserialize when you continue your game by loading that file.

Look at following image. After serializing ‘SerializationBox’ in the above sample code, I opened the output in a hex editor. You can use Notepad++ and hex plugin to open the serialized file.

Let us look at contents byte by byte and find out what they are. It starts with “ac ed”. It is is called STREAM_MAGIC. It is a magic number (java API guys says) that is written to the stream header. It denotes that is start of serialzed content.

Similarly every character has a meaning. Actually the serialized file is more bulkier than you would expect, as it has a huge header the meta information of the classes involved and finally the content. Object Serialization Stream Protocol have a look at chapter 6.4.2 Terminal Symbols and Constants. It gives you list of symbols and constants used in serialization.

Decrypting Serialized Java Object

In the image, I have underline a unit of information in a separate color for you to easily identify.

Interfaces with empty bodies are often used as markers to tag classes as having a certain property or behavior. Such interfaces are also called ability interfaces. Java APIs provide several examples of such marker interfaces: java.lang.Cloneable, java.io.Serializable, java.util.EventListener.