Public classBase {/*Public Base () {System.out.println ("initial construction method of the Base class"); }*/ PublicBase (intx) {System.out.println ("Overloaded construction method for Base class"); } Public voidA () {System.out.println ("Base:a ()"); } Public voidB () {//This is the class in which the object itself This. A (); } Public Static voidMain (string[] args) {Base obj=NewBase (2); OBJ.B (); }}
Overloaded construction method for Base class Base:a ()
Public classSubextendsbase{ PublicSub () {//The default is to call a constructor with no arguments, because the parent class does not have a constructor with no arguments, so the constructor that calls with parameters is displayed//super (); //calling the parent class's constructor method Super(100); //calling a method of the parent class Super. A (); System.out.println ("Initialize constructor method for sub class"); } Public Static voidMain (string[] agrs) {//the construction method of the parent class is called first when the subclass object is constructedSub obj =NewSub (); }}
The overloaded construction method of the Base class Base:a () The initialization construction method of the Sub class
Java Learning----This and super (in inheritance)