Why do you need to redefine serialVersionUID if you extend the class that implements Serializable "down the line"?

For one of my projects, I need to define a new exception that extends ProviderMismatchException.

From the javadoc link, you can see that this is an exception:

  • extends IllegalArgumentExceptionwhich
  • extends RuntimeExceptionwhich
  • extends Exceptionwhich
  • the extends Throwable.

All of them define their own static final serialVersionUID, with the exception of the Throwablemodifier that adds it private.

Now, if you implement the Foo interface, then all inherited classes also implement this interface, which means Serializable; however, why do subclasses in the JDK override it for each subclass? What is the danger of not defining it again for inherited classes?

+4
source share
1 answer

serialVersioUID is a static long value, so it will not be inherited through the class hierarchy.

This is to indicate whether the previous serial instance of the class has the same version as the current implementation or not.

serialVersionUID, serialVersionUID , Java (TM). , , serialVersionUID, serialVersionUID , , , InvalidClassExceptions .

. :

serialVersionUID ?

+1

Source: https://habr.com/ru/post/1619662/


All Articles