In the subclass, must I access the method with parameters of the parent class ?, A subclass accesses a private member of the parent class.
Abstract class Person {private int age; private String name;Public Person (intAge, String name){This. age = age; this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public abstract void want ();} class Student extends Person {private int score; public int getScore () {return score;} public void setScore (int score) {this. score = score ;}Public Student (int age, String name, intScore){Super (age, name );This. score = score;} public void want () {System. out. println ("name:" + getName () + "Age:" + getAge () + "score:" + getScore ());}} class Worker extends Person {private int money; public int getMoney () {return money;} public void setMoney (int money) {this. money = money ;}Public Worker (int age, String name, intMoney){Super (age, name );This. money = money;} public void want () {System. out. println ("name:" + getName () + "Age:" + getAge () + "Salary:" + getMoney ());}} public class ABSDemo01 {public static void main (String [] args) {Student s = new Student (10, "James", 100); s. want (); Worker w = new Worker (35, "Ming", 1000); w. want ();}}
Analyze the above Code:
The above code overwrites the constructor public Person (int age, String name) in the parent class ). If the sub-class does not access the parent class's constructor with parameters, the sub-class will access the non-argument constructor super () of the parent class by default (), however, the parent class has constructed a constructor with parameters,If there is no constructor in the parent class, it cannot be called, so compilation will fail.
Conclusion 1:
Java ConstructorCan only be called by new, The programmer cannot directly call, but it is availableSuper () indirect callThe class construction method isNot inherited!
Conclusion 2:
If the sub-class constructor does not show that the parent class constructor is called, the system automatically calls the default constructor super () of the parent class ().
Conclusion 3:
After a constructor is created, no default constructor is available. If no constructor is available, a constructor without parameters is created by default.
Conclusion 4:
When no constructor is provided for the class, the system automatically provides a constructor with no arguments for its implementation as null. Once a custom constructor is provided, the system will not provide this default constructor. To use it, the programmer must manually add it.