title: Java object Serialization and inheritance
body: <p>Say you have these two classes, Foo and Bar where Bar extends Foo and implements <code>Serializable</code></p>
  
  <pre><code>class Foo {
  
  public String name;
  
  public Foo() {
      this.name = "Default";
  }
  
  public Foo(String name) {
      this.name = name;
  }
  }
  
  class Bar extends Foo implements java.io.Serializable {
  
  public int id;
  
  public Bar(String name, int id) {
      super(name);
      this.id = id;
  }
  }
  </code></pre>
  
  <p>Notice that Foo doesn't implement <code>Serializable</code>. So what happens when bar is serialized?</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
  
      FileOutputStream fStream=new FileOutputStream("objects.dat");
      ObjectOutputStream oStream=new ObjectOutputStream(fStream);
      Bar bar=new Bar("myName",21);
      oStream.writeObject(bar);
  
      FileInputStream ifstream = new FileInputStream("objects.dat");
      ObjectInputStream istream = new ObjectInputStream(ifstream);
      Bar bar1 = (Bar) istream.readObject();
      System.out.println(bar1.name + "   " + bar1.id);
  
  } 
  </code></pre>
  
  <p>it prints "Default   21".
  The question is, why the default constructor get called when the class is not serialized?</p>