Knowing the difference between Externalizable and Serializable is essential as an interview question and we can use the knowledge to make better-informed decisions for performance improvement for applying serialization to our application.
1. Externalizable Vs. Serializable
Let’s list down the main differences between Externalizable and Serializable interfaces in java.
Serializable | Externalizable |
---|---|
Serializable is a marker interface i.e. does not contain any method. | Externalizable interface includes two methods writeExternal() and readExternal() which implementing classes MUST override. |
Serializable interface passes the responsibility of serialization to JVM and its default algorithm. | Externalizable provides control of serialization logic to the programmer – to write custom logic. |
Mostly, default serialization is easy to implement, but has a higher performance cost. | Serialization done using Externalizable , add more responsibility to the programmer but often results in better performance. |
It’s hard to analyze and modify class structure because any change may break the serialization. | It’s easier to analyze and modify class structure because of complete control over serialization logic. |
Default serialization does not call any class constructor. | A public no-arg constructor is required while using Externalizable interface. |
Please note that Externalizable
interface is the child interface of Serializable i.e. Externalizable extends Serializable. So if any class implements Externalizable interface and overrides its writeExternal() and readExternal() methods then the first preference is given to these methods over the default serialization mechanism provided by JVM.
Read More : How to override default serialization mechanism in java
2. Read More about Externalizable and Serializable
- More Efficient Serialization with Externalizable in Java
- Java Serializable Interface Implementation Guide
- How Deserialization Process Happen in Java?
Drop me your questions in the comments section related to Externalizable and Serializable interfaces in Java.
Happy Learning !!