public class ExtAA extends AA {
static int iTime;
public static void main(String argv[]) {
ExtAA d = new ExtAA();
d.func(iTime);
}
public static void func(int iTime) {
System.out.println(iTime);
}
public ExtAA() { }
}
class AA {
public AA() { System.out.println("AA"); }
}
prints:
AA
0
I would have expected public ExtAA() { } to override the constructor for AA and thus not print AA, just 0. Could someone explain in what way I am wrong, and if I’d want to override the constructor how could I ?
P.S. It is completely possible that my question is stupid, but I don’t get what public ExtAA() { } should or could do. This was at a test, I messed up and I’d like to know what’s actually happening (yes, I did go into debug and go over it step by step, I just don’t know why new ExtAA uses AA instead of it’s own defined constructor)
Constructor of super class in Java is always called. You can’t stop that. You can just control which constructor is called if there are more of them.
Please consider reading some basic tutorial of Java, this is elementary thing of Java OOP.
Implicitly your constructor looks like this: