Anon
Anon

Reputation: 2656

Explanation of output

My program

class Building {
Building() {
    System.out.print("b ");
}

Building(String name) {
    this();
    System.out.print("bn " + name);
}
};

public class House extends Building {
House() {
    System.out.print("h "); // this is line# 1
}

House(String name) {
    this(); // This is line#2
    System.out.print("hn " + name);
}

public static void main(String[] args) {
    new House("x ");
}
}

We know that compiler will write a call to super() as the first line in the child class's constructor. Therefore should not the output be:

b (call from compiler written call to super(), before line#2

b (again from compiler written call to super(),before line#1 )

h hn x

But the output is

b h hn x

Why is that?

Upvotes: 1

Views: 229

Answers (5)

Ted Hopp
Ted Hopp

Reputation: 234795

When a constructor starts with a call to another constructor (either this or super), the compiler does not insert a call to the superclass's default constructor. Thus, the calls tree is:

main
 \-> House(String)                (explicit call)
      |-> House()                 (explicit call)
      |    |-> Building()         (implicit call)
      |    |    |-> Object()      (implicit call)
      |    |    \-> print "b "
      |    \-> print "h "
      \-> print "hn x"

Upvotes: 5

PermGenError
PermGenError

Reputation: 46408

Here is the visual contol flow of your code:

new House("x ")---> House(args)---> House() --->Building()--->Object()
                               ^^this()    ^implicit      ^implicit super() call 
                                             super()
                                             call

---> stands for invoking

Output: b(from building no-args), h(from no-args House), hn x (from args House) b h hn x

From what I know, implicit call to super should be before this(), right? Line#2, in my code

EDIT:

The first line in the constructor is either a call to super class constructor using super() or a call to overloaded constructor using this(). if there is a call to overloaded constructor using this() there will be no call to super().

Upvotes: 1

Jeremy D
Jeremy D

Reputation: 4855

House(x) -> House() + "hn" + "x"
            Building() + "h" + "hn" +"x"
            "b" + "h" + "hn" + "x"

The call to the superclass will be called only once.

If you want Building(string name) to be called, you have to call it explicitly.

I think it could be easier for you to use super() instead of this()

Upvotes: 0

kosa
kosa

Reputation: 66637

As per JLS 8.8.7

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();"

Upvotes: 3

Brian Driscoll
Brian Driscoll

Reputation: 19635

Your House(string name) constructor calls House(), which in turn calls Building(). Building(string name) is never called.

If you wanted to explicitly call Building(string name), in your House(string name) constructor you could add this: super(name); instead of this();

Upvotes: 1

Related Questions