Suppose I have 1 base class and a derived class.
The derived class has extra fields that the base class doesn’t.
I then instantiate the derived class and assign it to a definition of the base class.
What happens when I serialize and deserialize the object via the base class.
For example:
Class TypeA{
int var1;
}
Class TypeB extends class TypeA{
int var2;
}
Class X{
public TypeA obj = new TypeB();
}
If I now serialise “obj” does var2 get included?
Yes, serialization doesn’t depend on the type of the reference (which
objactually is, a reference I mean) but on the type/class of the object being referenced, which is stillTypeB. If you callobj.getClass()it will returnTypeB.classand that’s what serialization will see as well.