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

6

37

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) ?

Weevily answered 4/2, 2012 at 16:45 Comment(2)
Not to sound snarky, but it would take less time to just try this than it took you to write this question.Cytosine
Not to sound snarky, but this would be a very, very small website if that advice were heeded consistently.Heartsease
M
25

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.

Melanite answered 4/2, 2012 at 16:49 Comment(5)
thanks, any idea how to get rid of the yellow eclipse warning about no serial version id declared?Weevily
More robust way will be to define is as private static finalMelanite
I still think it can be good to implement Serializable on the subclass for clarity. Just like how ArrayList implements List while also extending AbstractList which already implemented List.Blowup
You should use the private modifier on serialVersionUID.Osiris
Just to be clear... is it serialVersionUID or serialVersionUid? We have both here.Duhamel
P
15

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

Preconcerted answered 23/7, 2015 at 10:44 Comment(0)
T
10

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.

Tiliaceous answered 13/12, 2013 at 18:30 Comment(0)
C
8

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 serialVersionUIDs 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.

Cytosine answered 4/2, 2012 at 16:56 Comment(4)
I think serialVersionUIDs is declared for JVM to identify the change in class. and JVM can get it from base class so it shouldn't make any difference.Melanite
Well yeah, but my point really was, don't treat the serialVersionUID as an annoyance you're trying to get rid of. Learn why it's there and treat it appropriately.Cytosine
Yes, but we can have two different classes with same serialVersionUID. if it was a simple class without extending any class explicitly, then it's should be mandatory.Melanite
my thinking was that the old files won't be compatible if I try to load it with new modified classes... so instead I wil never change the classes ever... (this is relatively simple classes .. I just have a list of them I'm serializing)... so just to be safe I put a bunch of string and int and boolean fields and called them "reserved1,2,etc." .. in the unlikely case I will need to attach more data to the class in the future... but so file will be exact same format stillWeevily
I
3

No, because B already implements it through its base class A. That's what inheritance is all about.

Interphase answered 4/2, 2012 at 16:49 Comment(0)
A
3

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

Antagonist answered 23/2, 2017 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.