7 differences between Serializable and Externalizable interface in Java

In the previous Java tutorial I have discussed about Serialization using Serializable interface and Extrenalizable interface is simply an another interface to do the same work i.e. serialize and deserialize a class and its fields but with a slight variations in its implementation and also the functionality.

Before you go if you don’t have information regarding Serialization in Java then first read a few paragraphs from the Linked article – Serialization using Serializable interface.

Serialization vs Externalization in Java
Serialization vs Externalization in Java

Jump to specific part

  1. Difference b/w Serializable and Externalizable Interfaces
  2. Which interface should we use

So why the hell we need Externalizable interface when we have a Serializable interface?
And yes that’s a wise question and this tells that there should be something that can not be done effectively by using the Serializable Interface and vice versa.

Difference b/w Serializable and Externalizable Interfaces

So lets discuss what are the differences in both the interfaces and how to decide which one should be used –

  1. Serializable Interface is based on a  recursive algorithm i.e during the serialization besides the fields it will serialize all the objects that can be reached through its instance variables i.e. all the objects that can be reached from that object (provided that all the classes must implement Serializable Interface). This includes the super class of the object until it reaches the “Object” class and the same way the super class of the instance variables until it reaches the “Object” class of those variables. Basically all the objects that it can read. And this leads to a lot of overhead when we want to save only few variable or a small data as compared to the class
    • For eg – If you have a class named Mercedes and you just want to store the car series and its car identification number then you can not stop at this only and will have to store all the fields of that class and also of its super class(if exists and implements serializable interface) and a lot more.
  2. Serializable is a marker interface and hence no need to override any method and whenever there is any change in the entity or bean classes you just need to recompile your program whether in the case of Externalizable interface you have to implement writeExternal() and readExternal() methods which contains the logic to store and retrieve data and with changes you might need to do changes in the code logic.
  3. Serializable provides you both options i.e. you can handle the process by your own or you can leave it for the process to be done in the default way but in Externalizable you have to provide the logic of the process and have full control over the serialize and deserialize process.
  4. Serializable involves reflection mechanism to recover the object. This also adds the the metadata i.e. class description, variable information etc of all the serializable classes in the process which adds a lot of data and metadata into the stream and consumes bandwidth and a performance issue.
  5. A public no-arg constructor is needed while using Externalizable interface but in Serializable it reads the required information from the ObjectInputStream and this is why it uses reflection mechanism.
  6. You need to define serialVersionUID in case of Serializable and if it is not explicitly defined it will be generated automatically and it is based on all the fields, methods etc of the class and it changes every time you do the changes in the class. You if current id does not match with generated id you will not be able to recover the previously stored data. Since the ID is generated every time it will take considerable amount of time which is not a case with externalizable interface.
  7. Externalizable interface is fast and also consumes less memory as compared to the other one. Follow this link to see test results.

Which interface should we use

Now the main point is which interface should be used in which condition and the truth is no one can tell you that. You have to decide which interface suits your requirement and which one is suitable for you in the current scenario.
In a single project you can mark some classes with Serializable interface and some other with Externalizable interface and I would recommend you to use Serializable interface for most of the classes and use Externalizable for only those –

  • In which you want complete control over serialization process.
  • You are very much concerned about bandwidth and time consumed.
  • Your class requires a lot of serializing and deserializing operations as it will save bandwidth.

Do revert in case of any query or suggestions.

Keep learning. Happy Learning 🙂

Recommended -

Notify of
Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

Nice work 🙂

Sankalp Suryawanshi
Sankalp Suryawanshi
8 years ago

Can you please explain how readObject / writeObject from ObjectOutput/InputStream come into picture when customizing default serialization behavior as Serializable is marker interface, though these methods are defined to customize.

6 years ago

Thank you so much… Its very useful

Punit Jain
Punit Jain
6 years ago

It is explained in a great manner.

Would love your thoughts, please comment.x