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

ycomp picture ycomp · Feb 4, 2012 · Viewed 16k times · Source

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

Answer

Azodious picture Azodious · Feb 4, 2012

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.