Difference between Serializable and Externalizable in Java

In this tutorial,  we are going to see differences between Serializable and Externalizable interface in Java.

Java Serialization Tutorial:

Before understanding difference, let me provide a short note on Serializable and Externalizable.

Serializable :

It is marker interface and you do not have to provide any methods if you implement this interface. When if any class implement Serializable interface, JVM will take care of Serializing that object.

Externalizable:

As the name suggest it is externalizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshaling and unmarshalling of objects.Externalizable interface extends Serializable interface. If you implement this interface, you need to provide implementation of readExternal() and writeExternal() method.

Externalizable vs Serializable:

Parameter
Serializable
Externalizable
Marker interface
It is marker interface. You don’t have to provide implementation of any method.
Externalizable is not marker interface, you have to override writeExternal and readExternal method.
Control
Serializable interface has less control over serialization process and it is optional to override readObject and writeObject.
Externalizable interface has more control over serialization process and it is mandatory to override writeExternal and readExternal.
Performance
JVM uses reflection to perform serialization in the case of Serializable interface which is quite slow.
Programmer have to implement readExternal and writeExternal methods but it relatively results in better performance
Supersedes
NA
If you implement Externalizable interface and provide implementation of readExternal and writeExternal then it supersedes readObject and writeObject methods in that class. It is due to the fact that Externalizable extends Serializable interface.
Constructor called during Deserialization
Default constructor is not called during Deserialization process.
Default constructor is called during Deserialization process.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *