Suppose I have a Serializable
class ShapeHolder
that owns an object that implements a Serializable
Shape
interface. I wa
import java.io.*;
public class ExampleSerializableClass implements Serializable {
private static final long serialVersionUID = 0L;
transient private Shape shape;
private String shapeClassName;
private void writeObject(ObjectOutputStream out) throws IOException {
shapeClassName = shape.getClass().getCanonicalName();
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
in.defaultReadObject();
Class> cls = Class.forName(shapeClassName);
shape = (Shape) cls.newInstance();
}
}