Do you know how to customize the default behaviour of the serialization protocol? Tom Cruise starrer MI4 hits theatres this week in India and we experience unusual media frenzy. In no way this is realted but by impulse I mention about MI4 here. In this article I am going to write on how to customize Ghost protocol, oops serialization protocol.

One of my reader was asked a quesion in an interview on how to customize the serialization behaviour and this article serves as an answer for that question.
In general there are three approaches to serialization in java:
- Implement Serializable and use default protocol.
- Implement Serializable and get a chance to modify the default protocol.
- Implement Externalizable and write your own protocol to implement serailization.
Here protocol means, the way (process or approach) object is serialized and de-serialized.
We have seen enough about using the default protocol to implement serialization and how instances are created during serialization. In this current article we shall see about modifying the default protocol.
import java.io.Serializable;
public class Lion implements Serializable {
private String sound;
public Lion() {
System.out.println("Lion's constructor invoked.");
setSound("roar");
}
public String getSound(){
return sound;
}
public void setSound(String sound){
this.sound = sound;
}
}
Above is a simple java class which we are going to use to demonstrate serialization. First let use do simple straight forward serialization and then make the customization in step 2.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class JavaSerialize {
public static void main(String args[]) throws IOException, ClassNotFoundException {
Lion leo = new Lion();
// serialize
System.out.println("Serialization done.");
FileOutputStream fos = new FileOutputStream("serial.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(leo);
// deserialize
FileInputStream fis = new FileInputStream("serial.out");
ObjectInputStream ois = new ObjectInputStream(fis);
Lion deserializedObj = (Lion) ois.readObject();
System.out.println("DeSerialization done. Lion: " + deserializedObj.getSound());
}
}
Output:
Lion's constructor invoked. Serialization done. DeSerialization done. Lion: roar
Above class just uses the standard mechanism and demonstrates serialization using Lion class.
Customize Java Serialization
We know Serializable is a java marker interface. When a class implements Serializable interface it gives information to the JVM that the instances of these classes can be serialized. Along with that, there is a special note to the JVM
look for following two methods in the class that implements Serializable. If found invoke that and continue with serialization process else directly follow the standard serialization protocol.
So this gives us a chance to write these two methods insided the Class that implements Serializable and you get a hook to the serialization process. You can write your custom code inside these two methods and customize the standard behaviour of serialization.
- private void writeObject(ObjectOutputStream out) throws IOException;
- private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
I have modified the Lion class to include these two methods and on the fly I change a property of Lion to demonstrate this. This is not overriding or overloading methods and this is a mechanism provided by serialization. These two included methods are declared private but JVM can access the private methods of an object. There is no change to the class that does the serialization and de-serialization.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Lion implements Serializable {
private String sound;
public Lion() {
System.out.println("Lion's constructor invoked.");
setSound("roar");
}
public String getSound(){
return sound;
}
public void setSound(String sound){
this.sound = sound;
}
private void writeObject(ObjectOutputStream out) throws IOException {
setSound("meow");
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}
Output:
Lion's constructor invoked. Serialization done. DeSerialization done. Lion: meow
















This is really great article. Your blog is just awesome.
Green Mile on December 19th, 2011 8:30 amSIr…!!!thank you so much for sharing this article..!
pooja on December 19th, 2011 10:16 amit clears my concept about serialization.
thank you.
Very Simple to understand. Thanks for sharing…
Dev Ghotkule on December 19th, 2011 11:15 amIn order to control de-serialization of a Singleton object, which is serializable, we can add a method readResolve():
// This method is called immediately after an object of this class is deserialized.
// This method returns the singleton instance.
protected Object readResolve() {
return singleton;
}
While de-serializing, JVM calls this method, so you can always return the same object always inspite of the Serializable object being Singleton.
Tilak on December 20th, 2011 6:46 amThanks Tilak.
Joe on December 20th, 2011 9:56 amAs usual, awesome article. Now that you are so much getting into serialization, can we expect one more article explaining the use of “serialVersionUID” in serialization?
Just a small request. See if that can be fulfilled.
And keep the good work as usual. ;-)
Goldest on December 20th, 2011 1:54 pmhi,
i am bit confused with de-serialization. can u explain wat really happens while de-serializing the object and how constructor’s behave.
vadhiraju on December 29th, 2011 2:40 amGood Article on Serialization and Deserialization .
JavabynataraJ on January 5th, 2012 12:44 pmThis s a real good stuff..
Ganesh on January 9th, 2012 7:18 amThanks for this article, could you please explain about Externalizable in you own way for my better understanding.
Amir on January 10th, 2012 7:24 pmThanks in Advance.
Good Explanation. Thanks
Anonymous on January 17th, 2012 8:17 amGood one
Mahesh on February 14th, 2012 10:38 amThanks a ton dude.
steven on February 16th, 2012 10:19 amThanks. this is really awesome
ravikumar kotta on March 15th, 2012 9:40 amSir,
Deepak on April 8th, 2012 10:43 amcan I write normal static class
If Not why
and i am able to write a inner static class why it is allowed ?