Skip to content
Advertisement

Why does super() not get called in the other constuctor?

If both House() and House(name) are called then why is not “Building” printed two times, but instead one time?

class Building {
    Building() {
        System.out.println("Buiding");
    }
    Building(String name) {
        this();
        System.out.println("Building: String Constructor" + name);
    }
}
class House extends Building {
    House() {
        System.out.println("House ");
    }
    House(String name) {
        this();
        System.out.println("House: String Constructor" + name);
    }
}
public class Test {
    public static void main(String[] args) {
        new House(" OK");
    }
}

Output:

Buiding
House
House: String Constructor OK

Advertisement

Answer

House(String name) calls this() which is the same as House(). House() implicitly calls super() which is the same as Building().

You never call Building(String name).

House(String name) could call super(name) to do that.

Advertisement