title: Java serialization - java.io.InvalidClassException local class incompatible
body: <p>I've got a public class, which implements Serializable, that is extended by multiple other classes.  Only those subclasses were ever serialized before - never the super class.</p>
  
  <p>The super class had defined a serialVersionUID.</p>
  
  <p>I'm not sure if it matters, but it was not marked private, but rather it just had the default protection - you might say it was package protected</p>
  
  <pre><code>static final long serialVersionUID = -7588980448693010399L;
  </code></pre>
  
  <p>The super class, nor any of the subclasses, however implemented readObject or writeObject, and none of the subclasses had an explicitly defined serialVersionUID.  I figured one defined in the superclass would be sufficient.</p>
  
  <p>Despite all this, things were fine as far as reading back previously serialized objects until a new instance variable, a List/ArrayList, along with a new method was added to the super class, and some private instance variables were added to one of its subclasses.</p>
  
  <p>Now when trying to read back previously serialized objects, an exception is being thrown.  One similar to this:</p>
  
  <pre><code>com.SomeCompany.SomeSubClass; local class incompatible: stream classdesc serialVersionUID = 1597316331807173261, local class serialVersionUID = -3344057582987646196
  </code></pre>
  
  <p>I'm assuming this is caused because the default serialVersionUID, which was used because I didn't declare one in any of the subclasses, has now changed due to the changes in the superclass and one subclass.</p>
  
  <p>Suggestions on how to get out of this dilemma would be appreciated.  I'm assuming I need to implement readObject and writeObject, but other than invoking defaultReadObject() and defaultWriteObject(), I'm not exactly sure what I need to do.  Nor do I know if I need to add serialVerisonUIDs to all of the subclasses or if readObject and writeObject need to be implemented by each subclass, or if I can just implement them once, assuming I need to at all, in the superclass.</p>