Java Foundation-parent class-Subclass execution order

Source: Internet
Author: User

Code parsing

Sub-class

 Packagecom;/*** Sub-class *@authorHuage **/ Public classTestextendstest1{ Public Static voidMain (string[] args) {NewTest ();//To test the subclass and parent overload relationships and call relationships    }         PublicTest () {System.out.println ("Test"); Test ("Test"); Super. Test ("test");//the methods in the parent class are overloaded, so a method in the parent class that must be super can be called to execute    }    /*** Reload the test method in the parent class*/     Public voidTest (String name) {System.out.println ("Who:" +name+ "; Test:test"); }    /*** Static method does not call does not execute*/     Public Static voidtest1 () {System.out.println ("Test static void"); }}

Parent class

 Packagecom;/*** Parent class *@authorHuage **/ Public classTest1 { PublicTest1 () {System.out.println ("1"); //When a quilt class is overridden, a method in a subclass is called when the subclass is instantiatedTest ("Test1");  This. Test ("Test1"); //test1 ();    }    /*** When instantiating an object as a subclass, there is a subclass in which this method is overridden, the method that is called by the Quilt class in the original (parent class), the subclass calls the method that is overridden by the subclass * such as a method in the subclass to call the parent class requires super to call the * itself in this class cannot invoke the /c6>*/     Public voidTest (String name) {System.out.println ("Who:" +name+ "; 1:1"); }    /*** The static method is not overridden/overwritten, but can have the same method in the subclass, and in the call itself this is called its own method*/     Public Static voidtest1 () {System.out.println ("Test1 Static 111"); }        /*** The final method cannot be overridden/overwritten, the subclass cannot have the same method (method name, return type, parameters in the same case)*/     Public Final voidtest2 () {System.out.println ("Test2 Final 11111"); }}

Status of implementation

1who:test1;test:testwho:test1;test:testtestwho:test;test:testwho:test; 1:1

Summary description:

Subclasses inherit the parent class, and when the subclass is instantiated, according to how the JVM is loaded in the order of loading (from low-up), execution executes the instantiation in the parent class, and the contents of the child class are executed.

Java Foundation-parent class-Subclass 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.