If both House()
and House(name)
are called then why is not “Building” printed two times, but instead one time?
JavaScript
x
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:
JavaScript
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.