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
Jump to specific part
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 –
- 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.
- 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
readExternal() methods which contains the logic to store and retrieve data and with changes you might need to do changes in the code logic.
- 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.
- 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.
- 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.
- 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.
- 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