我一直以为构造函数不能被继承,但是看看这个代码:Java构造函数继承?
class Parent {
Parent() {
System.out.println("S1");
}
}
class Child extends Parent {
Child(){
System.out.println("S2");
}
}
public class Test5 {
public static void main(String[] args) {
Child child = new Child();
}
}
//RESULT:
//S1
//S2
这表明,孩子继承的构造。为什么有S1结果?有没有可能创建2个没有参数的构造函数,只有Child构造函数的结果没有基础构造函数(只有S2)?