Serialization is a process of writing the state of
an object to a byte stream.
Serialization is also needed to implement Remote
Method Invocation (RMI). RMI allows a Java object on one machine to invoke a
method of a Java object on a different machine. An object may be supplied as an
argument to that remote method. The sending machine serializes the object and
transmits it. The receiving machine deserializes it.
Assume that an object to be serialized has
references to other objects, which, in turn, have references to still more
objects. This set of objects and the relationship among them form a directed
graph.
If one attempt to serialize an object at the top of
a graph, all the other referenced objects are recursively located and
serialized.Similarly, during the process of deserialization, all of these
objects and their references are correctly restored.
The interfaces and classes that support
serialization are as follows:
o Serializable:Only an object that implements the Serializableinterface can be saved and
restored by the serialization facility. The Serializable interface defines no members. It is simply used to
indicate that the class can be serialized. If a class is Serializable, all of
its subclasses are also Serializable.
Variables
that are declared as transientare
not saved by the serialization facility. Also, static variables are not saved.
o Externalizable:The Java facilities for serialization and
deserialization have been designed so that much of the work to save and restore
the state of an object occurs automatically. There are situations in which the
programmer may need to have control over these processes. For example, it may
be desirable to use compression or encryption technique. The Externalizable interface is designed
for these situations.
The
Externalizableinterface defines two
methods:
voidreadExternal(ObjectInputinStream)
throws IOException, FileNotFoundException
voidwriteExternal(ObjectOutputoutStream)
throws IOException
Here, inStream is a byte stream from which the
object is to be read, and outStream is a byte stream
No comments:
Write comments