简体   繁体   中英

Hierarchy of classes in serialization and deserialization

I have a class hierarchy as follows:

public class A implements Serializable {}
public class B extends A {}
public class C implements Serializable extends B {}
public class D extends C {}

Is my judgment correct?

When serializing class D , all classes in the hierarchy are serialized because sub-classes are Serialized.

When deserializing class D , no default constructor is needed and the whole hierarchy is deserialized.

You are correct. Since the top-level class A implements Serializable , and classes inherit interfaces from their superclasses, all of your classes are serializable. No default constructors are necessary because all of the classes indicate, by being serializable, that they can be safely reconstituted by reading their state from the stream.

In Serialization class D flow, [is] all hierarchy serialized because sub-classes are Serialized.

No, it is all serialized because the base class A is Serializable .

In Deserialization class D flow, any no parameter constructor not need and all hierarchy deserialized.

Correct. The nearest non-serializable base class of A would need a no-args constructor, but in this case it is java.lang.Object , which does.

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