Why Externalizable when read/writeObject method are there in serializable

622 views Asked by At

I am not able to clear this confusion of mine,

Externalizable is used because we can give our own (more efficient) implementation of serialization-deserialization using read/writeExternal method. And control the serialization process.

We can also use read/writeObject method and serialization process will be overridden with that.

Then why is Externalizable even required?

Why would someone prefer read/writeExternal over read/writeObject?

1

There are 1 answers

6
Suresh Atta On

Just the customization in case of serialization.

The reason is given in Externalizable docs

The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods. Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream. **