Search code examples
javaserializable

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


Solution

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