Why does the program print the height value 0 instead of the one I set?

It's because you derived the Derived class from Base class and shadowing the variable and also overriding the methods.

You're calling the constructor of the Base class whenever you're instantiating the Derived class with:

Derived d = new Derived();

Here what happens when you're calling the above code:

  • Constructor of Base class Base() is called,
  • then "Inside Base Const" is printed,
  • method showAll() is not called because it's being override. Method showAll() inside the Derived class is called instead,
  • "Inside Base showAll" is printed,
  • "Derived : 106.0" is printed because double height = 196.0; inside Base class is being shadowed by double height = 106.0; inside the Derived class.

An important point to note is, when subclass object is created, a separate object of super class object will not be created.

Only a subclass object is created that has super class variables.

so we unable to blindly say that whenever a class constructor is executed, object of that class is created or not. Please refer to below changes and see.

public class Derived extends Base {
    public static void main(String args[]) {
        System.out.println("Hello World");
        Derived d = new Derived();
        d.getClass();
    }

    protected Derived() {
        System.out.println("Inside Derived Const");
        System.out.println("Sub class object hashcode :" + this.hashCode());
        System.out.println(this.getClass().getName());
        showAll();
    }

    protected void showAll() {
        System.out.println("Inside Derived showAll");
        System.out.println("Sub class object hashcode :" + this.hashCode());
        System.out.println(getClass().getName() + " : " + height);
    }

    double height = 106.0;
}

class Base {

    protected Base() {
        System.out.println("Inside Base Const");
        System.out.println("Super class object hashcode :" + this.hashCode());
        System.out.println(this.getClass().getName());
        showAll();
    }

    protected void showAll() {
        System.out.println("Inside Base showAll");
        System.out.println("Sub class object hashcode :" + this.hashCode());
        System.out.println(getClass().getName() + " : " + height);
    }

    double height = 196.0;
}

Output

Hello World
Inside Base Const
Super class object hashcode :1917513796
Derived
Inside Derived showAll
Sub class object hashcode :1917513796
Derived : 0.0
Inside Derived Const
Sub class object hashcode :1917513796
Derived
Inside Derived showAll
Sub class object hashcode :1917513796
Derived : 106.0
  • As we can observe that both super class(Base) object hashcode and subclass(Derived) object hashcode are same, so only one object is created.

  • This object is of class Derived as when we try to print name of class which object is created, it is printing "Derived" which is subclass.

  • When you have called showAll() in super class for first time it doesn't have a value for height variable because of the method showAll has overridden.But the value has not assigned at that line it calls.

  • When the method showAll() has called inside the subclass it has the value assigned which is 196.0. This is due to variable hiding*.

(Variable hiding: When the child and parent classes both have a variable with the same name, the child class' variable hides the parent class' variable.)