0

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 ?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Srinivas
  • 9
  • 7
  • 1
    Sad truth is: you "pretty well do not know". When programming; do not assume; and do not believe that your assumptions trumpet observed reality. But the good thing is: you came here ... and the core point is: your child classes **always** call super constructors; but most of the time you are not aware of this, because the "invisible" default constructors are called (inserted into your programs by the compiler without you doing anything). – GhostCat May 02 '16 at 17:01
  • Please read http://stackoverflow.com/questions/18415488/why-constructors-are-not-inherited. It says constructors cannot be inherited. – Srinivas May 02 '16 at 17:04
  • Correct; constructors are not **inherited**. But nonetheless, any "child" **absolutely** must call one constructor from its super class within each and any of its own constructors (and do so right as first statement). In that sense: if you are interested in language details, don't spend too much time here. Keep in mind that the **ultimate** truth is the Java Language Specification; and if you mean real business: try to find your answers in there yourself. You will be surprised how many things you can learn from that document. – GhostCat May 02 '16 at 17:11
  • 1
    And finally: serialization is somehow a bit of black magic. When objects are de-serialized, NO constructors are called. The JVM somehow brings your object into existence; and then fills it fields from serialized bytes. So, yes, you are partially right; and I missed that part. – GhostCat May 02 '16 at 17:13

1 Answers1

0

The parent constructor must be called by the subclass's constructor, however this does not always require an explicit super() call.

If the parent class has a default (no-argument) constructor, then it will automatically be called by the subclass constructor if you omit the call to super().

Darth Android
  • 3,437
  • 18
  • 19