1

Suppose you have the following structure (simplified):

interface Inter
{
   //...
}

class Impl1 implements Inter, Serializable
{
   private static final long serialVersionUID = ...;
   //...
}

class Impl2 implements Inter, Serializable
{
   private static final long serialVersionUID = ...;
   //...
}

class MyClass implements Serializable {
   private static final long serialVersionUID = ...;
   Inter interInstance; // can be Impl1, Impl2...
}

Later on, you add a new implementation:

class Impl3 implements Inter, Serializable
{
   private static final long serialVersionUID = ...;
   //...
}

...and end up with three classes that MyClass.interInstance can assume.

Should that have any impact in MyClass objects that are already serialized?

I'm asking this because users (Android app) have been complaining about not being able to open some Serialized objects after an update in which the changes mimic the example above (i.e. I only added a new serializable implementation of the interface).

user1987392
  • 3,921
  • 4
  • 34
  • 59

1 Answers1

1

Should that have any impact in MyClass objects that are already serialized?

I'm little confused from your question but if objects were serialized as Impl1 or Impl2 and you're trying to deserialize as Impl3 - this won't work. So conclusion is when you'll serialize object as Impl1, it has to be deserialised as Impl1.

By the way, especially in Android - officially supported and also recommended way how to "serialize" any kind of object(s) is an usage of Parcelable interface. And why to use it.

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • What I mean is that you can have a field in MyClass (interInstance) which can be Impl1 or Impl2 (both serializable). Later you add a third implementation (Impl3) and now interInstance in a MyClass instance can be either Impl1, Impl2 or Impl3. The question is whether this addition can have an impact while reading serialized objects from a file, created before Impl3 existed. – user1987392 Apr 10 '14 at 08:59
  • @user1987392 It depends on what you mean by impact. Serialized objects won't be changed but how i wrote. If you serialized object as Impl2 it has to be deserialized also as Impl2 and not for instance as Impl3. – Simon Dorociak Apr 10 '14 at 09:02
  • By impact I mean will a ObjectInputStream still be able to read (old) files where the serialized object has Impl1 or Impl2 as its interInstance. – user1987392 Apr 10 '14 at 09:34
  • @user1987392 i think there won't be problem with it. – Simon Dorociak Apr 10 '14 at 09:45