Object initialization process for Java parent class subclasses

Source: Internet
Author: User
Tags instance method

Summary

The basic Java object initialization process, the initialization of subclasses, and situations that can cause confusion when conversions involving parent classes and subclasses are involved.

1. Basic initialization process:

The initialization process for a simple class is:

Static modified modules (static variables and static blocks) are---> executed sequentially in code order.

|

instance variables and non-static modules---> executed sequentially in code order.

|

Constructor---> executes the corresponding constructor.

The initialization process for subclasses.

The parent class static decorated module

|

Sub-class static modifier module

|

Parent class instance variables and non-static blocks

|

The parent class corresponds to the constructor. The default constructor for the parent class is called when the child class does not display a call in the corresponding constructor.

|

Subclass instance variables and non-static blocks

|

Sub-class constructors

The static module of class is unique, so it is initialized only once. The static module of the common class for instances of all classes.

Initialization condition of the static module: (From: Reference 1)

(1) When encountering new, getstatic, Putstatic, or invokestatic 4 bytecode instructions, the initialization is triggered if the class is not initialized. The corresponding scenario is when new instantiates an object, reads or sets a static field (which is final decorated, except for the static field where the compiler puts the result in a constant pool), and when a static method of a class is called

(2) When the class is reflected

(3) initializes the subclass. When the parent class is not initialized, the parent class is first triggered.

(4) When the virtual machine starts, you need to specify a main class (including the main method) that needs to be executed, and the virtual opportunity initializes the class first

2. About covering and hiding

Overwrite: The overridden method is equivalent to being erased and unrecoverable (even if the type cast cannot be called).

Hide: A location that has been saved, and can continue to be used when needed (type cast can be called after).

About overriding and hiding relationships between parent class subclasses: (From: Reference 2)

1. Instance variables and class variables of the parent class can be hidden by a variable of the same name in the Quilt class.

2. Static method of the parent class A static method of the same name as the quilt class is hidden, and An instance method of the parent class overrides the instance method of the quilt class with the same name.

3. You cannot use a static method of a subclass to hide an instance method of a parent class, or to overwrite a static method of a parent class with an instance method of a subclass, or the compiler will be an exception.

4. The final method modified with the final keyword cannot be overwritten.

5. The variable can only be hidden from being overwritten, the instance variable of the subclass may hide the class variable of the parent class, and the class variable of the subclass can also hide the instance variable of the parent class.

Typical cases involved are:

Premise:

-----------------------------------------

Class B extends a{...}

-----------------------------------------

(1) b INSTANCEB = new B ();

The initialization process for INSTANCEB is the first part: the initialization of subclasses.

At this point Instanceb actually has all the information about a super (except the overridden instance method in the parent class), but the current reference is the subclass information (such as variables and methods that are not in the fruit class are inherited from the parent class).

(2) A instancea = new B ();

At this point, the variables and static methods of the parent class A hide the variables and static methods of the child class. Instancea the only subclass B that can be called at this time is the instance method in subclass B that overrides the parent class A.

Execution B instanceb = (b) instancea; After

At this point instanceb is equivalent to b INSTANCEB = new B ();

Attention:

(1) Here is the only case of overriding: the instance method of the parent class is overridden by the instance method of the quilt class with the same name.

When the method invokes the variable, it is actually accompanied by a default this. That is, the value of the current class is called in this instance method. When it comes to inheritance, pay great attention to the situation of super,this.

(2) At initialization time, there are two stages. The first step is to apply for space, and the second step is to assign a value.

See details:

The difference between the value of static and the final static value at initialization time. (citation 1)

When a subclass overrides a parent class instance method, the instance method of the subclass is called, and the instance method of the child class invokes the variable in the subclass (at which point the static variable is initialized, but the instance variable is not initialized). (citation 2)

This code comes from reference 2, how refined, so interesting. Worth a look.

Classparent{int x=10;PublicParent() {Add (2); }voidAdd(IntY) {x+=y; }    }ClassChildExtendsparent{int x=9;voidAdd(IntY) {x+=y; }public static < Span class= "Hljs-keyword" >void   Main (String[] args) {               parent p=new child ();               system.out.println (p.x);         }      }                

Reference 1. Class loading mechanism:

http://blog.csdn.net/kai_wei_zhang/article/details/8234146

Reference 2. Hide and overwrite, as well as a good example:

Http://www.cnblogs.com/binjoo/articles/1585342.html

Reference 3. Hide and overwrite

http://renyanwei.iteye.com/blog/258304

Reference 4. The basic initialization process

Http://www.cnblogs.com/miniwiki/archive/2011/03/25/1995615.html

Object initialization process for Java parent class subclasses

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.