"Error:implicit Super constructor Pet () is undefined. Must explicitly invoke another constructor "
Remember one point: when you construct a subclass, you are bound to call the constructor of the parent class. Because the elements in the parent class also need to be initialized.
So the parent class either has a default parameterless construct, so Java automatically calls this parameterless construct. If the parent class does not have a parameterless construct, then you call the construction of the parent class by Super () in the construction of the child class.
The reference code is as follows;
- Package practise02_2;
- /*
- * Subclasses overriding parent class methods
- */
- public class Pet {
- Private String PetName;
- Private String ownername;
- Pet () {}
- Public Pet (String petname,string ownername) {
- System.out.println ("Wweqwq");
- This.petname=petname;
- This.ownername=ownername;
- }
- public void Playwithowner () {
- System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
- }
- Public String Getpetname () {
- return petname;
- }
- public void Setpetname (String petname) {
- This.petname = PetName;
- }
- Public String Getownername () {
- return ownername;
- }
- public void Setownername (String ownername) {
- This.ownername = ownername;
- }
- }
- when a subclass is instantiated, it must initialize the parent class variable, which is called the parent class constructor **********************
- Package practise02_2;
- public class Cat extends Pet {
- Private String PetName;
- Private String ownername;
- Public Cat (String petname, String ownername) {
- Super (PetName, ownername); //comment out will error! Why?? When instantiating subclasses, the attributes in the parent class also need to be initialized!! Error resolved after adding parameterless construction method to parent class
- This.petname=petname;
- This.ownername=ownername;
- }
- public void Playwithowner () {
- System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
- }
- }
- "The private property of the parent class can be inherited??? "********
- Package practise02_2;
- public class Dog extends pet{
- It is worth noting that although there is no subclass attribute defined here, it does not mean that the subclass inherits the private property of the parent class
- Subclasses simply call the constructor of the parent class, and when the child class is instantiated, the value is passed to the parameter of the subclass constructor method
- As can be verified: the formal parameters of the subclass construction method are different from the parent class, and there is no relationship between the two
- Public Dog (String A, string b) {//Parameter only, no practical meaning
- Super (A, b);
- TODO auto-generated Constructor stub
- }
- public void Playwithowner () {
- System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
- }
- }
- main function ***************
- Package practise02_2;
- public class Test {
- public static void Main (string[] args) {
- Cat Cat=new Cat ("Jen", "Tom");
- Dog Dog=new Dog ("Wang Choi", "Tom");
- Cat.playwithowner ();
- Dog.playwithowner ();
- }
- }
- Run result ***********
Java constructor When you construct a subclass, you are bound to call the constructor method of the parent class. "Private property is inherited?" "Problem