Externalizable vs Seriablizable

Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization. protocol and provides out of the box serialization capabilities.

Externalizable extends Seriablizable.

Implement writeExternal and readExternal methods of the Externalizable interface and create your own contract / protocol for serialization.

Saving the state of the super types is responsibility of the implementing class.

You might have seen in my previous article on how to customize the default implementation of Seriablizable. These two methods readExternal and writeExternal (Externalizable) supersedes this customized implementation of readObject and writeObject.

In object de-serialization (reconsturction) the public no-argument constructor is used to reconstruct the object. In case of Seriablizable, instead of using constructor, the object is re-constructed using data read from ObjectInputStream.

The above point subsequently mandates that the Externalizable object must have a public no-argument constructor. In the case of Seriablizable it is not mandatory.

Behaviour of writeReplace and readResolve methods are same for both Seriablizable and Externalizable objects. writeReplace allows to nominate a replacement object to be written to the stream. readResolve method allows to designate a replacement object for the object just read from the stream.

In most real time scenarios, you can use Seriablizable and write your own custom implementation for serialization by providing readObject and writeObject.

You may need Externalizable,

1. If you are not happy with the way java writes/reads objects from stream.

2. Special handling for super types on object construction during serialization.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: