当父类只有有参构造函数时
这个时候子类一定要在自己的构造函数中调用父类的有参构造函数,不然会发生错误
public class father {
public father(String name){
System.out.print(name);
}
public static void main(String[] args){
son1 son=new son1();
}
}
class son extends father{
public son(){
super("sss");
}
}
class father1{
public father1(){
System.out.print("father1构造函数");
}
}
class son1 extends father1{
public son1(){
}
}
当父类拥有自己定义的无参构造函数和有参构造函数时
子类中可以显式或者隐式调用父类的无参构造函数,也可以调用有参构造函数
public class father{
public father(){
System.out.print("father构造函数");
}
public father(String name){
System.out.print(name);
}
}
class son extends father{
public son(){
//隐式调用
}
}
class test{
public static void main(String[] args){
son son1=new son();
}