Serialization can be done using two interfaces - one is Serialization In Java interface and the other is
Externalizable interface. While Serializable is convenience interface which saves and restores every member variable of the class, Externalizable helps in controlling what gets saved and restored.
class Student implements Serializable For e.g., in the Student class if we do not want to store the section variable, then we can implement Externalizable interface to achieve this. The Student class can be rewritten as
class Student implements Externalizable Externalizable . They are readExternal and writeExternal . writeExternal is used while serializing the object/writing to the external source like file, network or some other input stream. Where as readExternal is used for de-serializing/reading the data from external sources. Note that we need to define a constructor for the de-serialization to work. If there is no constructor as shown at LINE A , then while de-serializing, it throws InvalidClassException .
An example showing how the Student object can be serialized is explained at Java Serialization Process -.
|