My previous article on exploring java serialization is a box office hit. In continuation to that and popular request, I am going to write on how instances are created during serialize and de-serialize process.
There is a mystery. If you strongly believe that the constructor of a class is called everytime when an object is instantiated, then you have got a surprise.
- RULE1: When you serialize an object, the whole inheritance hierarchy is serialized till the first non-serializable class of that object.
To understand the above, consider the two example scenarios given through diagram,
Serialization Inheritance Hierarchy Example 1

Serialization Inheritance Hierarchy Example 2

- Vehicle is the first non-serializable class.
- Animal is the first non-serializable class.
Therefore, when you serialize an object of HybridCycle or Lion, as a chain it will save the state till Vehicle and Animal classes respectively.
Animal class must have an empty constructor. In the case of Cycle inheritance chain, Vehicle is the last class. Vehicle implicitly extends Object and thus gets access to an empty constructor.
- RULE2: The first non-serializable class must have an empty constructor.
This rule makes sense when you de-serialize. When you de-serialize, the object is not constructed using it’s constructors. Objects are instantiated using the saved state when it was serialized. Deserialization stops at the first non-serializable class and the empy constructor will be used.
In de-serialization,
- For Vehicle scenario, Object’s empty constructor will be used.
- Animal must have an empty constructor. If you have a parameterized constructor in Animal, you must explicitly add the empty constructor or you will get compilation error.





















Such an useful post. The concept is very clear by the examples and by the explanation which is very simple to understand. Thank You.
Giftvincy on December 12th, 2011 6:48 amThank you Joe. The explanation is great. Now I am gonna try to read all your posts.
mesut on December 12th, 2011 7:16 amVery nice article. Clear our serialization concept.
Rajeev on December 12th, 2011 12:59 pmGood article
Jasdeep Singh on December 12th, 2011 1:27 pmreally a great post…thank you javaguy
Vinod Gulia on December 15th, 2011 11:59 amReally Nice Information..
Chetan Pagar on December 15th, 2011 2:09 pmThank you very much for sharing it.. :)
Very nice article…Thank you for sharing
meen on December 28th, 2011 6:42 amgood article
Dhiraj on January 3rd, 2012 9:30 amI joe, thank you for sharing such good post. But i have doubt in 2nd rule you specify must have default constructor is not clear to me.
Akhtar on January 4th, 2012 5:30 amNice ..article with images.Thanks..
JavabynataraJ on January 5th, 2012 12:46 pmVery crisp and clear…
can you please post some article on Annotations.
Anonymous on January 7th, 2012 4:44 amGreat and interesting explanations!!!
Kat on January 9th, 2012 7:58 pmSimple explanation of a very simply presented concept.
Jayant on January 17th, 2012 12:00 amThis defenitions and post and example was very very good and interesting to read and easily understable, please keep post like this.
Thanks a lot Joe
Raja on January 27th, 2012 6:10 pmHi Joe,
Thanks for the sharing good java material. I have gone through lot of articles on this site and they are quite useful. I slight disagree with the information provided on this page. I don’t think the rules apply for serialization as such. These rules are related to inheritance not serialization
tarun on February 14th, 2012 9:41 pm