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

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.


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