子类调用父类默认构造函数

来源:互联网 发布:网络日语翻译兼职 编辑:程序博客网 时间:2024/05/18 03:53

看一个题目:

    class Person {          String name = "No name";          public Person(String nm) {              name = nm;          }      }      class Employee extends Person {          String empID = "0000";          public Employee(String id) {              empID = id;          }      }      public class Test {          public static void main(String args[]) {              Employee e = new Employee("123");              System.out.println(e.empID);          }      }  

选项:
A输出:0000
B 输出:123
C 编译报错
D No name

答案:C

==================================================

解析:子类的构造函数默认会调用父类的构造函数。如果不显示的调用,则调用父类的
默认构造函数(无参构造函数),如果父类构造函数不是无参构造器,则子类构造器必须显示的调用。

0 0