Is serialVersionUID inherited by subclasses, if I have default serialVersionUID in superclass [closed]

StackOverflow https://stackoverflow.com/questions/23465613

  •  15-07-2023
  •  | 
  •  

سؤال

Is serialVersionUID inherited by subclasses, if I have default serialVersionUID in superclass? Similar to when the superclass is serializable then subclasses are also serializable.

I had a superclass initially with no default serialVersionUID so I was getting:

local class incompatible: stream classdesc serialVersionUID = -3473908186986930430, 
local class serialVersionUID = -7527159820765531130

So I added this to the superclass:

private static final long serialVersionUID = 1L;

My question is: does serialization consider serialVersionUID from superclass when serializing subclasses or not. Do I need to specify serialVersionUID in every subclass explicitly?

هل كانت مفيدة؟

المحلول

Is serialVersionUID inherited by subclasses, if I have default serialVersionUID in superclass?

No it is not inherited, because it is private, and in any case Serialization won't consider it as belonging to the subclass and not use it.

Similar to when the superclass is serializable then subclasses are also serializable.

It isn't similar. Serializable is an interface and it is subject only to the rules of the language. serialVersionUID is a special field with its own rules enforced by ObjectInputStream.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top