Sub-class object instantiation process

Source: Internet
Author: User

The instantiation of a subclass is divided into two main steps:

<1> class-related static content initialization;

* My father's class again sub-class:

1. Static property of the parent class;

2. Static blocks of the parent class;

3. The static property of the subclass;

4. Static blocks of subclasses;

<2> The instance content initialization of the. class;

* Call constructor: The late father subclass: Instantiate instance fields and blocks before constructing a child call

5. Normal properties of the parent class;

6. Ordinary blocks of the parent class;

7. The constructor of the parent class;

8. Generic properties of subclasses;

9. Ordinary blocks of subclasses;

10. Sub-class constructor;

1  Public classClzinit {2      Public Static voidMain (string[] args) {3         NewSon ();4     }5 }6 7 classParent {8     protected intn = 5;9     protected Static intm = 5;Ten  One     Static { Am = m * 2; -System.out.println ("Parent class static block invocation; M= "+m); -     } the  -     { -n = n * 2; -m = m * 2; +System.out.print ("Normal block Call of the parent class; "); -System.out.print ("n=" +n); +System.out.println ("m=" +m); A     } at  -      PublicParent () { -          This. N = n * 10; -m = m + 10; -System.out.print ("Parent class constructor"; N= "+n); -System.out.println ("m=" +m); in     } - } to  + classSonextendsParent { -     Private intsn = 3; the     Private Static intSM = 3; *  $     Static {Panax Notoginsengm = m + 2; -SM = SM + 2; theSystem.out.println ("sub-class static block invocation; M= "+m); +     } A  the     { +n = n + 2; -sn = sn + 2; $m = m + 2; $System.out.print ("Subclass generic block invocation; "); -System.out.print ("n=" +n); -System.out.print ("sn=" +sn); theSystem.out.println ("m=" +m); -     }Wuyi  the      PublicSon () { -          This. N = n + 10; Wusn = sn + 10; -m = m + 10; AboutSystem.out.print ("sub-class constructor; N= "+n); $System.out.println ("m=" +m); -     } -}

Operation Result:

Parent class static block invocation;     M=10 class static block invocation;  M=12 common block invocation of the parent class;  n=10  m=24 parent class constructor;  n=100  m=34 subclass common block invocation;  n=102  sn=5  m=36 sub-class constructor;  n=112  m=46

In addition, if a subclass overrides a method of the parent class, and the static property, static block, normal property, or normal block of the parent class contains this same name method, the child class is actually called the overridden method rather than the parent class's original method, although it occurs in the parent class order stage.

As follows:

 Public classMain { Public Static voidMain (string[] args) {NewSon1 (); }}classFather1 {intK =foo (); Static{System.out.println ("Father static Block"); }     PublicFather1 () {System.out.println ("Father Constructor"); }     Public intfoo () {System.out.println ("Father initializing"); return1; }}classSon1extendsFather1 {intK1 =foo (); Static{System.out.println ("Son static."); }     PublicSon1 () {System.out.println ("Son constructor."); }     Public intfoo () {System.out.println ("Son initializing."); return1; }}

Operation Result:

Staticstaticson initializingfather Constructorson Initializingson Constructor 

Note that the bold line, whose order is the execution of the parent class normal property extreme, but because of the method involved in the subclass rewrite, actually called the subclass after the overridden method.

Sub-class object instantiation process

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.