Tuesday, January 8, 2019

How are constructors called during serialization and deserialization?

  1. When there is one class implementing serializable?

  2. When there is parent/child relationship and only child implements serializable?

  3. When there is parent/child relationship and both parent and child implements serializable?

    Ans :

    • 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).

     

No comments:

Post a Comment