This example shows how to use the Serializable Fields API with Serialization,
demonstrating that the class can define fields other than those already in
the class to be serializable. This differs from just rewriting the
writeObject
method to customize the data format (see the
Custom Data Format example)
because, in this example, versioning support still holds.
Using the Serializable Fields API, this example specifically changes the internal representation of a rectangle from x1,y1,x2,y2 implementation (see Original Class) to Point(x1,y1), Point(x2,y2) (see Evolved Class) while the external representation still remains x1, y1, x2, y2. This ensures bidirectional compatibility between the original and evolved representations.
As with all examples dealing with evolution, here the evolved class
(ARectangle) must define a serialVersionUID field that is obtained using
the command serialver
on the original class (ARectangle
).