简体   繁体   中英

If my class implements Serializable, do I have to implement it in its subclasses?

If I have B extends A... and A implements Serializable, do I have to write "B implements Serializable" ?

I think no, but I would like confirmation...

also if I put serialization id in A... do I need to put one in B also ? should serialization id in A be protected (not private) ?

Yes. Subclass need not be marked serializable explicitly.

And, marking id as protected will do (from compiler perspective).

But, as good practice every class should have it's own private serialVersionUID .

You don't have to explicitly mark the derived as Serializable it will be inherited. However, the serialVersionUID from the parent, although inherited, will not be used by the serialization process. If you don't add a serialVersionUID to the child one will be generated.

See Below:

public class A implements Serializable {
    protected static final long serialVersionUID = 1L;
}

public class B extends A {
}

public class Main {

    public static void main(String[] args){

        A a = new A();
        B b = new B();

        Class aClass = a.getClass();
        Class bClass = b.getClass();

        long aUid = ObjectStreamClass.lookup(aClass).getSerialVersionUID();
        long bUid = ObjectStreamClass.lookup(bClass).getSerialVersionUID();

        System.out.printf("serialVersionUID:\n");
        System.out.printf("b inherited from a: %d\n", b.serialVersionUID);
        System.out.printf("a used by serialization: %d\n",aUid);
        System.out.printf("b used by serialization: %d\n",bUid);
    }

}

Output:

serialVersionUID:

b inherited from a: 1

a used by serialization: 1

b used by serialization: -3675232183873847366

Marking id as protected will suffice from a compiler perspective. However, in theory, the point of the serialVersionUID field on a Serializable class is to easily distinguish "versions" of the class when deserializing it -- to clearly denote when a given object can be deserialized into an instance of the provided class (if the serialVersionUID s are different, an exception is thrown). If you want to be able to clearly track the versions and nature of an object, declare serialVersionUID on each subclass.

In my agreement to @azodious answer, child class inherits serializable properties of parent class , but you will have to declare serialVersionUID explicitly.

From Java docs: https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html

It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.

A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable.

You can take a look at official documentation and find this there: https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

不,因为B已经通过它的基类A实现了它。这就是继承的全部意义所在。

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