Controlling Java Serialization

How to make more control of the Serialization?

Classes that need to completely control the serialization
process can implement java.io.Externalizable, which extends
java.ioSerializable.

java.io.Externalizable declares readExternal() and writeExternal()
methods for you to implement and handle the serialized reading and writing.

This interface (java.io.Externalizable)
completely replaces the default serialization behavior for assembling and disassembling
the component elements. This involves the following:

An java.io.Externalizable class
must provide a no-argument contructor which is invoked before readExternal() method.

The readExternal() and writeExternal() methods must explicitly
read and write any data necessary to reconstruct the object.
(this includes data members from object of the class and all its objects of superclasses)

readObject() and writeObject() methods will not be called on the class
or any super class, and defaultReadObject() and defaultWriteObject() methods are not available.

The java.io.ObjectStreamClass and internal handle are used in the same way as
they are for java.io.Serializable classes

Example in how to completely control the serialization of java objects:

First a class, Rectangle, that is NOT made serializable and have no default constructor: