Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will serialization save the superclass fields? [duplicate]

My subclass implements Serializable, but my superclass does not.

Both subclass and superclass contain variables that need to be saved as part of the state of the subclass.

Will serialization save the superclass fields?

like image 922
Danijel Avatar asked May 08 '13 14:05

Danijel


People also ask

What are the disadvantages of serialization?

Since serialization does not offer any transaction control mechanisms per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.

What happens when a class is Serializable but its superclass is not?

Case 2(a): What happens when a class is serializable, but its superclass is not? Serialization: At the time of serialization, if any instance variable inherits from the non-serializable superclass, then JVM ignores the original value of that instance variable and saves the default value to the file.

What happens during serialization?

Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.

What happens if the object to be serialized?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.


1 Answers

A superclass fields cannot be serialized if it is not Serializable.Here is a summary of some rules of Java serialization:

  • An object is serializable only if its class or its superclass implements the Serializable (or Externalizable) interface.

  • An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the firstsuperclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.

  • The no-arg contructor of every non-serializable superclass will run when an object is deserialized. However, the deserialized objects? constructor does not run when it is deserialized.

  • The class must be visible at the point of serialization.

  • All primitive types are serializable.

  • Transient fields (with transient modifier) are NOT serialized, (i.e., not saved or restored). A class that implements Serializablemust mark -transient fields of classes that do not support serialization (e.g., a file stream).

  • Static fields (with static modifier) are Not serialized.

  • If member variables of a serializable object reference to a non-serializable object, the code will compile but a RumtimeExceptionwill be thrown.

like image 86
Juned Ahsan Avatar answered Oct 26 '22 01:10

Juned Ahsan