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