What is the effect of compiling the following code and then executing the 'Child' application?
1. public class Papa {
2. int i;
3. Papa(int j) {i=j;}
4. }
5.
6. class Child extends Papa {
7. Child() {i=5;}
8. public static void main(
String[] args) {
9. new Child();
10. }
11. }
A. Compiler error at line 6.
B. Compiler error at line 7.
C. Compiler error at line 9.
D. The code compiles but throws exception when the application is executed.
E. The code compiles and executes with no problems.
The answer is B. I can understand this. Since 'Papa' has non-default constructor, it does not get a no-parameter constructor at compile time. So line 7 will not compile, as the compile tries to call its superclass default constructor.
My question is: Will it compile if line 7 was:
Child(int k) { // do something
}
Does the compiler try to call the superclass no-parameter constructor only if the Child class has a no-parameter constructor?