Table of Contents
In this tutorial,  we are going to see differences between Serializable and Externalizable interface in Java.
Java Serialization Tutorial:
- Serialization in java
- Java Serialization interview questions and answers
- serialversionuid in java serialization
- externalizable in java
- Transient keyword in java
- Difference between Serializable and Externalizable in Java
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.
|
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?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.