After specifying a parameter constructor when creating a class, the system will not create a parameterless constructor by default, and it needs to be created manually by yourself.
When creating an object instance of a subclass, the parameterless constructor of the parent class (the default constructor) will be called by default.
If the parent class does not define a parameterless constructor, an error will be reported during the compilation stage.
If the subclass specifies the parameter constructor of the parent class, it can be compiled and run.
Subclass declaration super(id, city) shows that the parent class parameter constructor is called
package cn.lw.testpkg;/** * @author wanglei April 18, 2018*/class Predessor { private int id; private String city; public Predessor(int id, String city) { this.id = id; this.city = city; } @Override public String toString() { return "Predessor [id=" + id + ", city=" + city + "]"; }}class Successor extends Predessor { private String name; private String sex; public Successor(String name, String sex, int id, String city) { super(id, city); this.name = name; this.sex = sex; } @Override public String toString() { return "Successor [name=" + name + ", sex=" + sex + "]"; }}public class CallConstructorTest { public static void main(String[] args) { Successor s2 = new Successor("A", "male", 1, "HZ"); System.out.println(s2); }}Output
Successor [name=A, sex=male]
The parent class does not define a parameterless constructor, and the child class does not declare super(id, city)
public Successor(String name, String sex) { this.name = name; this.sex = sex;}Compilation error
Implicit super constructor Predessor() is undefined. Must explicitly invoke another constructor
The parent class displays the definition of parameterless constructor, the transitiveness of method calls
package cn.lw.testpkg;/** * @author wanglei April 18, 2018*/class Predessor { private int id; private String city; public Predessor() { System.out.println("Call the constructor without the parameter of Predessor"); } public Predessor(int id, String city) { this.id = id; this.city = city; } @Override public String toString() { return "Predessor [id=" + id + ", city=" + city + "]"; }}class Successor extends Predessor { private String name; private String sex; public Successor() { System.out.println("The constructor without parameters of Successor was called"); } public Successor(String name, String sex) { this.name = name; this.sex = sex; } @Override public String toString() { return "Successor [name=" + name + ", sex=" + sex + "]"; }}public class CallConstructorTest { public static void main(String[] args) { Successor s = new Successor(); System.out.println(s); System.out.println("-----------------------"); Successor s2 = new Successor("A", "male"); System.out.println(s2); }}Output
Called the constructor without parameters. Called the constructor without parameters.
Successor [name=null, sex=null]
--------------
Called the Predessor constructor without arguments
Successor [name=A, sex=male]
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.