Sep 8, 2015

Difference between Serializable and Externalizable in Java


Serializable vs Externalizable

Difference between Serializable and Externalizable is a famous core Java interview question. In this articles we will list down some differences between the two.
Although this article covers important differences, but its recommended to write down programs and see it yourself. Doing so will also clear the syntactical doubts, which is also important for interview point of view.

Difference between Serializable and Externalizable in Java 

Following is the list of main differences between Externalizable and Serializable interface in Java. 

1) Serializable is a marker interface i.e. does not contain any method but Externalizable interface contains two methods writeExternal() and readExternal().

2) 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) 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.

Hopefully you liked the article.
Don't forget to join us on +Java Territory  stay updated with latest articles.

0 comments:

Post a Comment