how to construct in inheritance://A inherits from B//1. Before new A A, you need to first construct a B//2. The "super ()" keyword is used to construct B in the construction method of a (the Super keyword needs to be placed in the first row of the construction method, since B is constructed before the next one is constructed)//3. When the Super keyword is not written in the A constructor method, the compiler automatically calls the parent class without the parameter constructor (regardless of whether the parent class has no Override)//4.super () different construction methods for parent class according to incoming parameters//5.super () in a multi-layer subclass is just a construction method in a layer-up class. For example: Public classclas1{ Public Static voidMain (string[] args) {C dx=NewC ();//Call Class C }}classA//Parent Class{ intA; PublicA ()//non-parametric construction{System.out.println ("This is the construction method of a 1"); } PublicAintSZ)//Pass-Parameter construction{System.out.println ("This is the construction method of a 2"); } }classBextendsA//b inherits from A, is the subclass of a{ intb; PublicB ()//non-parametric construction { Super();//running a class-a non-parametric construction method } PublicBintSZ2)//Pass-Parameter construction { Super(22);//To run a Class A-pass parameter construction method } }classCextendsB//C inherits from B and is a subclass of B{ intC; PublicC ()//C-Class construction method { Super(3);//Run Class B construction method, choose different construction methods depending on the parameters passed in }}
How to construct in Java inheritance