java - Will serialization save the superclass fields? -
this question has answer here:
- java object serialization , inheritance 3 answers
my subclass implements serializable, superclass not.
both subclass , superclass contain variables need saved part of state of subclass.
will serialization save superclass fields?
a superclass fields cannot serialized if not serializable.here summary of rules of java serialization:
an object serializable if class or superclass implements
serializable(orexternalizable) interface.an object serializable (itself implements serializable interface) if superclass not. however, firstsuperclass in hierarchy of serializable class, not implements serializable interface, must have no-arg constructor. if violated, readobject() produce
java.io.invalidclassexceptionin runtime.the no-arg contructor of every non-serializable superclass run when object deserialized. however, deserialized objects? constructor not run when deserialized.
the class must visible @ point of serialization.
all primitive types serializable.
transient fields (with transient modifier) not serialized, (i.e., not saved or restored). class implements serializablemust mark -transient fields of classes not support serialization (e.g., file stream).
static fields (with static modifier) not serialized.
if member variables of serializable object reference non-serializable object, code compile rumtimeexceptionwill thrown.
Comments
Post a Comment