Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 945
OldSchool Avatar asked Jul 07 '14 16:07

OldSchool


People also ask

What happens if your Serializable class contains a member which is not Serializable?

It'll throw a NotSerializableException when you try to Serialize it.

What will happen if a class doesnot implement Serializable interface and still we try to serialize object of that class?

If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.

What will happen if we don't implement Serializable?

The Student would not be Serializable, and it will act like a normal class. Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link.

Does a class need a constructor to be Serializable?

The only requirement on the constructor for a class that implements Serializable is that the first non-serializable superclass in its inheritence hierarchy must have a no-argument constructor.


1 Answers

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.

like image 161
AgilePro Avatar answered Nov 15 '22 19:11

AgilePro