Sunday, June 20, 2021

Difference between Serializable vs Externalizable in Java - Interview Question

Serializable vs Externalizable
What is the 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 wants to see in Java interview. I was in that category until I explored How Serialization works in Java and find out more about the Serialization process. What makes Serialization questions tricky is, Serialization as a persistence mechanism is not very popular. Many programmers prefer databases, memory-mapped files, or simple text files over Serialization. But Serialization has a distinguished advantage over these mechanisms.


Java provides a default mechanism to preserver object state and your job many times would be as simple as adding implements Serializable on any class. In this article, we will compare Serializable to Externalizable and see some common differences 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 points to explore on the topic of Serializable and refresher before going to any Java interview.




1) One of the obvious differences 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) The second difference between Serializable vs Externalizable is the responsibility of Serialization. when a class implements Serializable interface, the default Serialization process gets kicked off and that takes responsibility for serializing the superclass state. When any class in Java implement java.io.Externalizable then it's your responsibility to implement the Serialization process i.e. preserving all important information.

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

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

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


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

4 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

Feel free to comment, ask questions if you have any doubt.