Because the first line of every constructor, is a call to a super constructor. If you do not add it yourself (i.e. call a specific super constructor) the compiler automatically adds a call to the default constructor of the super class. And, yes, obviously, this call will finish before the constructor in which it is to be found. To be complete your code is actually as follows:
public class ClassA {
public ClassA() {
super(); // the default constructor of Object
System.out.println("Class A constructor");
}
}
public class ClassB extends ClassA {
public ClassB() {
super(); // the default constructor of ClassA
System.out.println("Class B constructor");
}
public static void main(String[] args) {
new ClassB(); // explicitly call ClassB constructor
}
}
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Thanks for that input masijade, I have found that the call to super() is often omitted in these circumstances, and that explicitly calling super() is redundant, because it is called implicitly. It does, however, improve code readability to show our call to super() as the first statement. So, good point and thanks for viewing! :)
majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
That is what I said, that super() is added by the compiler, if you don't add a super call yourself. I simply illustrated what the code effectively was. In fact, if your classes looked like this
public class ClassA {
public ClassA(String s) {
System.out.println("Class A constructor " + s);
}
}
public class ClassB extends ClassA {
public ClassB() {
System.out.println("Class B constructor");
}
public static void main(String[] args) {
new ClassB(); // explicitly call ClassB constructor
}
}
You will get the compiler message "Implicit super constructor ClassA() is undefined. Must implicitly invoke another constructor."
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
That's great, thanks for your input on this snippet. The clearer it is to others, the better!
majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
I just wan to add one point -
If you define a constructor in a class, you must have to define the default constructor(without parameter) also, otherwise you will get an compilation error.
Sorry, but not if you don't want that the "default constructor" be used, or when it is not applicable. Such as when a class revolves around a specific instance variable and that a class that doesn't have it defined is "useless" and a default value makes no sense, than a "default constructor" also makes no sense, so you define a constructor with which that value can be set and do not define a "default constructor". That way, any class extending that classmust explicitly call that super constructor, thereby keeping the "extended class" valid.
I.E. here is an example (a bit construed but valid)
package ages;
public class Age {
private int age;
public Age(int age) { this.age = age; }
public int getAge() { return age; }
}
---------------------------------------
package ages;
public class OldAge extends Age {
private static final int minAge = 65;
public OldAge() {
super(minAge);
}
public OldAge(int age) throws NotOldEnoughException {
super(age);
if (age < minAge) throw new NotOldEnoughException();
}
}
See, the Age classneeds an age, and what should be the default age? There is no sensible value, and an Age without an age value is also nonsensical, so there is no default constructor. Therefore, all constructors of OldAge must explicitly call the super(int) constructor.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494