Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
new B();
}
}
class A {
A() { System.out.print("A"); }
}
class B extends A {
B() { System.out.print("B"); }
}AB
- Expecting
B: a constructor that does not writesuper(…)still calls the parent's, because the compiler insertssuper()as its first statement. - Expecting
BA: the parent's constructor finishes first, so objects are built from the parent down to the subclass.
- What changes if the only constructor of
AisA(int x)? - In what order do the constructors run for a class
C extends B?