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
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