Externalization is nothing but serialization but by implementing Externalizable interface to serialize and deserialize the object.
Externalizable is a sub-interface to Serializable but it is not a marker interface because it has two unimplemented methods readExternal()
and writeExternal()
- which should be implemented by the classes which use Externalizable interface.
Serializable and Externalizable
Serializable is marker interface but Exterternalizable is not a marker interface it contains methods such as
void writeExternal(ObjectOutput out) throws IOException; void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
In Externalization JVM gives call back to readExternel()
and writeExternal()
of java.io.Externalizable
interface for restoring and writing objects into persistence.
Externalization provide complete control over the serialization and deserialization process it also presents challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java. If used correctly Externalizable interface can improve performance of serialization process.
Externalization with Example
Here follows a Contact class example with Externalizable interface. Here you need to note the order of reading from ObjectInput and writing to the ObjectOutput.
No comments:
Post a Comment