A Java Interface that shows an object can be serialized

Something is said to be serializable if it can be converted to a string and back again. This is a useful thing to be able to do, as the serialised thing can be sent to another machine through a TCP connection or modem, or written to (and later read from) a disk. While this concept is certainly not peculiar to OO, in OO languages it takes on a new dimension - objects combine code and data, so an object can specify how it is to be serialized. This means that something written to handle serializable objects can handle any serializable object without modification.

In Java, an object is Serializable if it implements the interface java.io.Serializable . For a class that has no state apart from its variables, all that is needed to make a class one is writing Serializable is to declare that the class implements the interface, and provide a no-argument constructor. The implementation that is inheirited from java.lang.Object is capable of serializing (and deserializing) any object of this type.

If the class has state apart from internal variables, it is possible to make it serializable by implementing the interfaces readObject and writeObject. For example, an Object representing a TCP connection to a server would not remain connected if it was serialized and deserialized using the default implementation. However, the class could provide its own implemention of readObject and writeObject - writeObject could cleanly disconnect from the server, and write out anything necessary to reconnect again; readObject could reconnect to the server, using the data previously serialized to restore the same (or an equivalent) session, possibly throwing an IOException if something goes wrong.

Implementing readObject and writeObject can be useful in cases other than when there is internal state that cannot be serialized. If the class saves lots of data that can easily be regenerated, but takes up a lot of space (a lookup table, for instance), writeObject can neglect to serialize the data, and readObject can regenerate it. Similarly, if data is easily compressible with a special-purpose algorithm (an ordered list of words, for example, can be compressed much better with a special-purpose algorithm1 than any general-purpose one), writeObject can compress the data, and readObject decompress it.

The interface java.io.Serial is defined as

private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

source: Java 1.3 Core API Specification ( http://java.sun.com/products/jdk/1.3/docs/api/ )

1 - Store each word as the part of the word that differs from the previous word, and a single character representing the number of characters on the front that are the same. For instance,

This can be further compressed by a general-purpose compression algorithm.

Log in or register to write something here or to contact authors.