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