Srinivas Palam wrote:If I replace this() with super(#2) why is it skipping printing h? What is the use of this(#1) in building class? It's just to call object constructor?
The first line in a constructor needs to be either a call to this or super. If neither of those are typed (meaning you explicitly add that code) then the compiler will add a call to the parents no-args constructor ( super();)
Furthermore one of the constructors in the class will eventually need to call super to instantiate the parent class.
So when you comment out the call to this() on line 12 and added the call to super there is no longer a need to execute the no-args constructor of House. Therefore "h" is not printed.
Regarding your second question, calling this(); on line 5 will simply call the no-args constructor. However in this sample code the Building(String name) overloaded constructor is never called.
Remember that the compiler will never call the super that contains parameters so this must be explicitly done
Example:
House(String name) {
// this();
super(name); #2
System.out.print("hn " + name);
}