Externalization example in Java

The Externalizable interface provides the necessary means for implementing a custom serialization mechanism. Implementing the Externalizable interface means that we must override writeExternal and readExternal methods. These methods will be called when you serialize or deserialize a particular instance. The difference between Serialization and Externalization is that when we implement the Serializable interface we do not need to implement any method; so the serialization takes place automatically. Even if we want to implement our own serialization mechanism by implementing the Serializable interface (yes it is also possible by defining writeObject and readObject methods) we don’t need to override or implement any method.
Serialization uses certain default behaviors to store and later recreate the object. You may specify in what order or how to handle references and complex data structures, but eventually it comes down to using the default behavior for each primitive data field.

Externalization is used in the rare cases that you really want to store and rebuild your object in a completely different way and without using the default serialization mechanisms for data fields. For example, imagine that you had your own unique encoding and compression scheme.

When we are implementing the Externalizable interface we must also define a default constructor otherwise serialization will be failed.

 

Thanks for your reading. Please leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.