I pretty well know that parent class constructor are not called from the subclass in inheritance. But looking in the below example it proves to be wrong. Could someone explain on this:
import java.io.*;
class Animal {
int i = 10;
Animal() {
System.out.println("Animal constructor called");
}
}
class Dog extends Animal implements Serializable {
int j = 20;
Dog() {
System.out.println("Dog constructor called");
}
}
class SerializableWRTInheritance {
public static void main(String[] args) throws Exception {
Dog d1 = new Dog();
d1.i = 888;
d1.j = 999;
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d1);
System.out.println("Deserialization started");
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d2 = (Dog) ois.readObject();
System.out.println(d2.i + "........." + d2.j);
}
}
The output is coming as follows:
Animal constructor called
Dog constructor called
Deserialization started
Animal constructor called
10.........999
I don't understand why Animal constructor has been printed over here. Could someone answer me the above ?