Home >Java >javaTutorial >Construction method of child parent class in JAVA inheritance
First of all, the constructor itself will have an implicit no-argument constructor (default):
① If the constructor is not written, the first line of code in the class will actually There is a default no-argument construct (the system will implicitly write it for you)
public class Student { private String name; // public Student() {} 隐式地“写”在这里,你看不见 // Strudent类中虽然没有写构造方法,但相当于有上面的无参构造 // 只不过是隐式的,你看不见 }
(Recommended video: java video tutorial )
②Write only the constructor method with parameters, which is equivalent to only the constructor method with parameters (the implicit parameterless construction will be shielded and ignored and regarded as invalid)
public class Student { private String name; public Student(String name) { this.name=name; } // 此时原来Strudent类中的隐式的无参构造方法被屏蔽了,无效了 // 类中只有带参构造 }
③If you want to have both parameterless and parameterized constructors, you must explicitly write the parameterless and parameterized constructor methods
public class Student { private String name; public Student() {} // 显式地将无参构造写出来 public Student(String name) { this.name=name; } // 若想Strudent类中拥有无参构造方法,必须显式地写出来 }
To further combine inheritance, you need to consider the child and parent classes:
④In the constructor method of the subclass (whether parameterless or parameterized), the first line of code in the method actually implicitly contains the parameterless constructor method of the parent class
That is: super()
public class Stu extends Student { private String name; public Stu() { // super(); // 在子类的无参构造中,super()是隐式的“写”在这里的 } public Stu(String name) { // super(); this.name=name; // 在子类的带参构造,上面的super()同样也是隐式的“写”在这里的 } }
This is why, when calling the constructor of a subclass, the parameterless constructor of the parent class will be called first, because the default super() exists.
⑤ Similarly, similar to ② above, if you write a parameterized construct at this time, super(xx) will block the implicit super()
public class Stu extends Student { private String name; public Stu(String name) { // super(); 原来隐式写在这里的super()被屏蔽了,无效了 super(name); // 在子类的带参构造, 由于的super(name)的存在,super()无效了 //此时子类的带参构造中,只有super(name) } }
This is why when When the parent class does not have a parameterless constructor (that is, it only has a parameterized constructor - corresponding to case 2), the subclass's constructor method cannot be compiled. This is because the child class's constructor (with or without parameters) will call the parent class's parameterless constructor. Since the compiler is trying to insert super() into 2 constructors in the child class, but the default constructor of the parent class is undefined, the compiler reports an error message.
To solve this problem, just
1) Add a parameterless constructor to the parent class - explicitly add the parameterless constructor to the parent class
2 ) Delete the custom parameterized constructor in the parent class - equivalent to restoring the default parameterless constructor
3) Add Super(XXX) to the subclass constructor - through the original of ⑤ Block the default super()
This article comes from the php Chinese website, java tutorial column, welcome to learn!
The above is the detailed content of Construction method of child parent class in JAVA inheritance. For more information, please follow other related articles on the PHP Chinese website!