What is the result?
11. public class Person {
12. String name = “No name”;
13. public Person(String nm) { name = nm; }
14. }
15.
16. public class Employee extends Person {
17. String empID = “0000”;
18. public Employee(String id) { empID = id; }
19. }
20.
21. public class EmployeeTest {
22. public static void main(String[] args){
23. Employee e = new Employee(“4321”);
24. System.out.println(e.empID);
25. }
26. }
A.
4321
B.
0000
C.
An exception is thrown at runtime.
D.
Compilation fails because of an error in line 18.
Explanation:
Main.java:18: cannot find symbol
symbol : constructor Person()
location: class Person
public Employee(String id) { empID = id; }
^
1 errorImplicit super constructor Person() is undefined. Must explicitly invoke another constructor
If you want a no-arg constructor and you’ve typed any other constructor(s)
into your class code, the compiler won’t provide the no-arg constructor (or any other constructor) for you. In other words, if you’ve typed in a constructor
with arguments, you won’t have a no-arg constructor unless you type it in
yourself!
You’re right. If a call to the super constructor is not explicit, then implicitly will be called the super constructor by default, which is without parameters; but in this case, the super class has no a constructor by default. Hence, either, must be specified explicitly a call to the existent super constructor, or must be implemented one by default in the super class.
Hence, correct answer is the option D.