8
public class Parent {

public Parent() {

System.out.println("Parent");

    }
}


public class Child extends Parent implements Serializable {


  public Child() {

      System.out.println("Child");
    }

}

public class Demote {

    public static void main(String[] args) {
      Child c=new Child();
       try {
        FileOutputStream fos=new FileOutputStream   
                                        ("D:\\DemoFile\\Testword5.txt");

        ObjectOutputStream oot=new ObjectOutputStream(fos);
        oot.writeObject(c);
        FileInputStream fin=new FileInputStream 
                                      ("D:\\DemoFile\\Testword5.txt");

        ObjectInputStream oin=new ObjectInputStream(fin);
        oin.readObject();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    }
 }

Output of this code is - Parent Child Parent.

The first set Parent Child is called because zero argument constructor is called. oin.readObject() is returning a child object . Then why only parent class's constructor is called and why not the child class

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Trishita
  • 81
  • 2
  • May be duplicate: http://stackoverflow.com/questions/8141440/how-are-constructors-called-during-serialization-and-deserialization – Rafiq Aug 17 '15 at 10:45

2 Answers2

2

The Parent constructor is called because it doesn't implement Serializable. The Child however does implement Serializable to it is not called.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Can you elaborate about why this makes sense? I mean given the question you have told why it works like this but what is the reason behind this design decision why it should work like this. Any idea? – Aseem Bansal Aug 17 '15 at 13:55
  • @AseemBansal The parent class cannot know that a sub-class has been marked as Serializable. So it is initialised in the normal way, via the constructor. However the sub-class knows the constructor need not be called as it is Serializable. – Peter Lawrey Aug 17 '15 at 20:00
1

Quoting from The Java Docs for Serializable:

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

The Serializable classes themselves do not need a no-args constructor, and it will not be used for initializing them if it exists.

Community
  • 1
  • 1
Hulk
  • 6,399
  • 1
  • 30
  • 52