Code
Public classTest { Public Static voidMain (string[] args) {NewCircle (); }} classDraw { PublicDraw (String type) {System.out.println (type+ "Draw Constructor"); }} classShape {PrivateDraw Draw =NewDraw ("Shape"); PublicShape () {System.out.println ("Shape Constructor"); }} classCircleextendsShape {PrivateDraw Draw =NewDraw ("Circle"); PublicCircle () {System.out.println ("Circle Constructor"); }}
Results
Shape Draw Constructorshape Constructorcircle draw Constructorcircle Constructor
This topic focuses on the sequence of calls and initialization of constructors at class inheritance. One point to remember is that the constructor invocation of the parent class and the initialization process must precede the child class. Because the Circle class's parent class is a shape class, the Shape class initializes first and then executes the constructor of the shape class. Then the sub-class circle is initialized, and the last circle's constructor is executed.
Java Foundation-Inheritance-subclass and parent class execution order