Thursday, March 12, 2009

If class A does not implement Serializable but a subclass B implements

Question :If class A does not implement Serializable but a subclass B implements
Serializable, will the fields of class A be serialized when B is
serialized? (CoreJava)

Answer :Only the fields of Serializable objects are written out and restored. The
object may be restored only if it has a no-arg constructor that will initialize
the fields of non-serializable supertypes. If the subclass has access to the
state of the superclass it can implement writeObject and readObject to
save and restore that state.

No comments: