Reputation: 48252
I have a class like this
class MyClass implements MyInterface, Serializable {
private static final serialVersionUID = 42;
...
}
interface MyInterface {
void A();
void B();
}
I have saved in a database some serialized instances of MyClass.
Now, I'm adding a new function to MyInterface so it becomes:
interface MyInterface {
void A();
void B();
void C();
}
And I have implemented C()
in MyClass.
Will my previously serialized instances deserialize as the new class with no problem? I think yes but wanted to confirm, if possible, with explanation.
Upvotes: 5
Views: 50
Reputation: 639
Yes, there should be no problems with adding a new function, so long as the serialVersionUID stays the same, as Java only keeps information about fields when serializing an object.
From ObjectOutputStream
:
State [of the serialized object] is saved by writing the individual fields to the ObjectOutputStream using the writeObject method…
Upvotes: 3
Reputation: 136002
If serialVersionUID is the same in the modified class serialization will try to do its best to deserialize data into the new class ignoring any differences.
Upvotes: 1