No, these two classes do not have the same serialVersionUID
. There is no exception thrown because class B
can be reconstructed successfully. There would for example be an InvalidClassException when the serialVersionUID
would not match or when A
would not implement a default constructor.
When you deserialise with objectInputStream.readObject()
, an Object
is returned which can be cast safely to B
. Since B
extends A
, it can be upcasted to an object of type A
– as it happens in your code.
On a sidenote, I strongly suggest that you define a serialVersionUID
for your classes if you are directly storing and retrieving objects using serialisation.
You can get the serialVersionUID
of the two classes as follows:
ObjectStreamClass.lookup(A.class).getSerialVersionUID()
ObjectStreamClass.lookup(B.class).getSerialVersionUID()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…