0

If I have two classes A and B:

  1. B inherits from A.
  2. B implement Serializable.
  3. A does NOT implement Serializable.
  4. B defines a serialVersionUID as is best practise.

If there in the future is implemented a breaking change in A and a developer forgets to update the serialVersionUID of B, then there will be serialisation runtime errors for B.

Any good tips and tricks to reduce the risk of this happening?

I did think of some options myself, but they are far from sufficient:

  1. Add a comment in A about remembering B in case of breaking changes. This is fine but not all developers carefully read the comments where they make changes.
  2. Make A Serializable too. This is bad because A does not need to be Serializable and it does not fix the issue. A can still implement breaking changes without updating B.serialVersionUID with the same error as effect. The only value is as a reminder about B, a bit like the comment mentioned above.
  3. Refactor B so it does not inherit from A. This would solve the issue, but seems extreme. There is also a good reason for them to inherit from each other.

The above points becomes even more costly and less likely to have the desired effect, if instead of just A and B then there is a whole chain of inheritance.

Thanks for reading this.

PS: I did try to google this question, but it is tricky to find anything.

RobbingDaHood
  • 150
  • 2
  • 2
  • 12
  • Your question is founded on a misconception. "If there in the future is implemented a breaking change in `A`": if `A` isn't `Serializable` there can't be any breaking changes to it. – user207421 May 24 '22 at 07:13
  • @user207421 When B inherits the fields of A then it will also inherit its breaking changes, right? – RobbingDaHood May 24 '22 at 07:14
  • Wrong, because `A` isn't serializable, and because it doesn't participate in computing the `serialVersionUID` of `B` anyway. – user207421 May 24 '22 at 07:16
  • @user207421 I just tested this locally and you are right. Wow that is dangerous. Thanks. If you add the above as an answer then I will approve. – RobbingDaHood May 24 '22 at 07:21
  • That is dangerous how? On the contrary, it is exactly what you want. – user207421 May 24 '22 at 07:23

0 Answers0