When a class [say A] is serialized all the subclasses which extends A will be serialized. Suppose in case of a scenario when the class A is extended by 2000 classes do the serialized object consists of all the subclass objects. By overriding the “private void writeObject” from subclass are we completely eliminating the parent object from getting serialized. Please confirm.
Thanks and Regards,
Subbu
No, the class
Aand its instance variables will be serialized, as long as the general contract of serialization is maintained.The serialized representation of a class does contain information about its supertypes. But not of its subclasses.
No, overriding
private void writeObjectdoes not prevent the parent object from being serialized. Reference the Javadoc: