简体   繁体   中英

Why base class(not implementing Serializable) should have no argument constructor if its subclass implements Serializable?

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

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

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

When you attempt to deserialize an serializable object, the mechanism must create an empty instance of the object, and fill in the members, to restore the object to the state it was when serialized. A constructor of a serializable object would have been called when the object was first constructed, but constructors are NOT called during the deserialization because, technically, you are not constructing the object, instead reconstituting it to the former state. Any effects of construction and subsequence manipulation are expected to already be incorporated into the object state.

Whenever you construct an object of any class, Java must call a constructor of the super class, and the super-super-class, etc. You can specify a specific constructor for the super class by using super(...) or if you don't specify a super constructor, the default constructor will be used. One way or another, all classes to the root are constructed.

Deserialization of serlializable objects do not cause constructor invocation, but when there is a super class that is not serializable, (that is you extend a non-serializable class with a serializable class) then that class is not expecting to be deserialized, and it has no mechanism for storing/restoring its members. If the super class is not serializable, the deserialization mechanism needs to call the zero-argument constructor to make sure that the reconstituted object instance is initialized correctly.

If you fail to specify a zero-argument constructor, the deserialization code will not warn you of this problem until your first attempt to deserialize an object of that class. There is no warning at compile time.

Furthermore, your serializable subclass must take responsibility for storing/restoring any member values from the non-serializable super class.

In case super class is not Serializable than to serialize the subclass's object we must implement serializable interface in subclass explicitly. In this case the superclass must have a no-argument constructor in it.

If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM