Wednesday, May 29, 2013

Marker Interfaces , Serialization and serialVersionUID


Marker Interfaces:

An interface contains only abstract methods where as a marker interface contains no methods at all.

yeah, Marker inerface is an interface which doesn't have anything (variables or methods) in it.

examples:

Some example of Marker interfaces are..

java.io.Serializable,

java.lang.Cloneable,

javax.servlet.SingleThreadModel etc..


Why Marker Interfaces:

As Marker interfaces contains no methods why we need to use marker interfaces..? The answer is to instruct the compiler

A marker interface is used to give instructions to the java compiler. Classes that implement these marker interfaces  indicates their special behaviour to compiler.  

As of Java 1.4 Marker inerfaces exists . From Java 5 onwards, we have the concept of annotations which are used to give instructions to java compiler. So marker interfaces are deprecated later.

For e.g, if we write a class which implement Serializable interface (marker), that means we are giving instruction to compiler such that this class is eligible for serialization.


What is Serialization:

Serialization is the process of making an object to be persisted in a stream and can make that object to be tranferred through the network.

Not clear...

Here is the simple definition..

Writing (storing) an object into a stream (e.g file stream) is called Serialization.


Example:

Create a class SerialObject which implement Serializable interface. We can observe that compiler will show the warning The serializable class SerialObject does not declare a static final serialVersionUID field of type long




What is serialVersionUID:

A serialVersionUID is an unique id used by the compiler while in the process of deserialization.

By using serialVersionUID  compiler will check the class that was already serialized is same or not while deserialising it.

i.e to confirm that the class is same while serializing (writing object into stream) and deserialising (reading object from streams).

If we have not provided the serialVersionUID serialization run time will assume some id to the class.

However, as per JAVA specification it is strongly recommended to declare a serialVersionID which should be a static final of type long to avoid unexpected exceptions like InvalidClassException.

Also it should be a private member so that even it's sub classes can't access it.

Our SerialObject class with serialVersionUID and some properties is as shown.



We have declared transient variables, name and notTrransferable.

Transient Variable:

A transient variable is a variable that can't be transferred through a network or we can say , a variable which can't be serialized. That means, we can't write this variable into streams. 

To limit writing data into streams we will use transient variables in serializable classes.


Serializing :

SerializingApp is the class which is showing the process of serialization ( writing object content into file stream).

To write an object we will take the help of java.io.ObjectOutputStream which will accept java.io.OutputStream as an argument.


If the file is available in the specified location it will use that, otherwise FileoutputStream will create a file with the specified name in the specified location.

writeObject() is used to write the object's content into stream.

If we observe the output in the created file write_here.txt, it will only contain objectName and objectId since the remaining are transient and can't be transferred though a network (can't be serialised).


DeSerializing:

Reading the content from a stream and representing it again in the form of an object is called DeSerialization.

DeserialisngApp is the class which is showing process of reading the object from specified file (deserialization).

To write an object we will take the help of java.io.ObjectInputStream which will accept java.io.InputStream as an argument.


we need to specify the location of the file where we have stored our object (SerialObject) to the FileInputStream.

You can clearly observe that the class (SerialObject) which we have stored in the file stream in serialization is the same as the class which we got in deserialization.

serialVersionUID is used by the run time, to confirm that the class is same in Serialization and Deserialization.





2 comments:

  1. Great Article..Got clear picture on serialization and deserialization. Keep blogging

    ReplyDelete
  2. If you want to take a great deal from this post then you
    have to apply these strategies to your won blog.

    ReplyDelete