Serialization, Evolution, and the Addition of a New SuperClass

Abstract

Shows how to use versioning in serialization to evolve the class to have a new superclass. For example, if the initial class structure was as follows:

  class A {...};
  class C extends A {...};

and the evolved class structure is as follows:

  class A {...};
  class B extends A {...};
  class C extends B {...};

then the new version of subclass C should be able to read the old version of subclass C, and vice versa. The example demonstrates this principle.

The evolved classses (ASuperClass and ASubClass) must define a serialVersionUID field that is obtained using the command serialver on the original classes (ASuperClass and ASubClass).

Note that in this example, the superclasses (A and B) implement the Serializable interface. If they did not, subclass C would be responsible for saving and restoring the fields of A and B. See Serialization with a NonSerializable Superclass for additional information about this topic.

Additional Information:

Source Files
Running the Example
List of Examples

Oracle and/or its affiliates
Java Technology

Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.

Contact Us