When a subclass inherits the parent class, if the parent class does not have a constructor that defines the parameter, the subclass can inherit the default constructor of the parent class
When a constructed method with parameters is defined in the parent class, the subclass must explicitly call the parent class's constructor method
If, at this point, the subclass also wants to invoke the default constructor method of the parent class, the default constructor must be explicitly declared in the parent class
1 PackageCom.gaohui;2 3 Public classTest {4 Public Static voidmain (String [] args) {5Man mans =NewMan ("Tom"));6 man.eat ();7Man.eat ("Tom");8 Man . Exercise ();9Woman Woman =NewWoman ();Ten One } A - } - the classpeople{ - Private intAge ; - PrivateString name; - PrivateString sex; + - Publicpeople () { + //if the parent class does not declare a default constructor method, the subclass must explicitly call the constructor of the parent class definition when it inherits A } at - PublicPeople (intAge , String name) { -System.out.println ("Construction method one executed!") "); - } - - Public voideat () { inSYSTEM.OUT.PRINTLN ("People need to eat!"); - } to + Public voidEat (String name) { -System.out.println (name+ "needs to eat!"); the } * $ Panax Notoginseng } - the classMansextendspeople{ + PublicMans (intAge, String name) {//because the parent class's constructor method is a parameter, you must explicitly call the parent class's method A Super(age,name); the } + - Public voidExercise () { $System.out.println ("I Love doing exercise!"); $ } - } - the - classWomanextendspeople{Wuyi the}
Error:implicit Super Constructor people () is undefined for default constructor. Must define an explicit constructor