Saturday, October 20, 2012

Difference between Serializable vs Externalizable in Java - Interview Question

Serializable vs Externalizable
What is difference between Serializable and Externalizable in Java is famous core Java interview questions and for some of them its one of those difficult Java question, which no one want to see in Java interview. I was on that category until I explored How Serialization works in Java and find out more about Serialization process. What makes Serialization questions tricky is , Serialization as persistence mechanism is not very popular. Many programmer prefer database, memory mapped file or simple text file over Serialization. But Serialization has a distinguished advantage over these mechanism. Java provides default mechanism to preserver object state and your job many time would be as simple as adding implements Serializable on any class.In this article we will compare Serializable to Externalizable and see some common difference between Serializable vs Externalizable to tackle this Java question.

Difference between Serializable and Externalizable in Java

Serializable vs Externalizable in Java DifferenceHere is my list of differences between Externalizable and Serializable interface in Java. These are good point to explore on topic of Serializable and refresher before going to any Java interview.

1) One of the obvious difference between Serializable and Externalizable is that Serializable is a marker interface i.e. does not contain any method but Externalizable interface contains two methods writeExternal() and readExternal().

2) Second difference between Serializable vs Externalizable is responsibility of Serialization. when a class implements Serializable interface, default Serialization process gets kicked of and that takes responsibility of serializing super class state. When any class in Java implement java.io.Externalizable than its your responsibility to implement Serialization process i.e. preserving all important information.

3) This difference between Serializable and Externalizable is performance. You can not do much to improve performance of default serialization process except reducing number of fields to be serialized by using transient and static keyword but with Externalizable interface you have full control over Serialization process.

4) Another important difference between Serializable and Externalizable interface is maintenance. When your Java class implements Serializable interface its tied with default representation which is fragile and easily breakable if structure of class changes e.g. adding or removing field. By using java.io.Externalizable interface you can create your own custom binary format for your object.

These are some of the important difference between Serializable vs Externalizable interface in Java. It's even better if you try to write Java program one with Serializable interface and other implementing Externalizable interface. Then you can evaluate all these differences between them

Other Java Interview articles you may find interesting:
Why Java does not support multiple inheritance?
Why wait and notify method are declared on Object class instead of Thread?
Difference between StringBuffer and String in Java
Can you override static method in Java
What is difference between Runnable and Thread in Java

3 comments:

  1. 4th point is really good

    ReplyDelete
  2. You might like to reference http://javarevisited.blogspot.com/2012/01/serializable-externalizable-in-java.html as your original posting.

    ReplyDelete
  3. Externalization interface allows you to define your own binary protocol for Serialization. But you can also customize customize serialization process you implement readObject() and writeObject() method from java.io.Externalizable interface. JVM checks implementation of these methods and call them instead of usual one .

    ReplyDelete

Java67 Headline Animator