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.
Agilepro
source share