Java Foundation-Inheritance-subclass and parent class execution order

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.