Why shouldn't a base class (not implementing Serializable) have an argument constructor if its subclass implements Serializable?

I am reading Serializable interface docs in which I find the following lines:

In order to allow serialization of subtypes of non-serializable classes, the subtype can take responsibility for maintaining and restoring the state of public, protected and (if available) supertype packages. A subtype can take this responsibility only if the class that it extends has an available no-arg constructor to initialize the state of the class. Error declaring a Serializable class if it is not. An error will be detected at runtime.

But what is the role of the no-arg constructor of the base class in restoring the state of an object?

+7
java serialization
source share
2 answers

When trying to deserialize a serializable object, the mechanism must create an empty instance of the object and populate it to restore the object to the state it was during serialization. The constructor of the serializable object is called when the object is first created, but the constructors DO NOT call during deserialization, because technically you are not creating the object, but restoring it to its previous state. It is expected that any effects of constructing and manipulating subsequences will be included in the state of the object.

Whenever you build an object of any class, Java must call the constructor of the superclass, super-superclass, etc. You can specify a specific constructor for the superclass using super(...) or if you do not specify a super constructor, the default constructor will be used. One way or another, all classes are fundamentally built.

deserialization of Serlializable objects does not cause a constructor call, but when there is a superclass that is not serializable (you are extending a non-serializable class with a serializable class), this class does not expect deserialization, and it does not have a mechanism for storing / restoring its members. If the superclass is not serializable, the deserialization mechanism must call the constructor of the null argument to ensure that the instance of the restored object is initialized correctly.

If you did not specify a constructor with a null argument, the deserialization code will not warn you about this problem until the first attempt to deserialize an object of this class. There is no warning during compilation.

In addition, your serializable subclass should be responsible for saving / restoring any element values โ€‹โ€‹from a non-serializable superclass.

+13
source share

In case the superclass is not Serializable than serializing the object of the subclass, we must explicitly implement the serializable interface in the subclass. In this case, the superclass must have a constructor with no arguments.

If the superclass is not Serializable, then all instance variable values โ€‹โ€‹inherited from the superclass will be initialized by calling the Non-Serializable Super class constructor during the deserialization process.

0
source share

All Articles