I have the following classes:
class A implements Serializable {
String field1;
String field2;
// a lot more fields here...
}
class B extends A {
int someOtherField;
}
where B is my class and A is in a package I don't own. In my environment, an object of class B is serialized and will later be deserialized in a different environment, where only class A is in the classpath and not class B, thus an error will occur, that class B is not found.
To solve this problem, I could add a copy constructor to A as follows and instead of serializing an object b, I would serialize new A(b):
A(A other) {
this.field1 = other.field1;
this.field1 = other.field1;
// and so on
}
My problem now is that I can only change B, but not A.
So, is there any way I can serialize a B and deserialize it as an A without access to the source code of A?