java-Preliminary Understanding-nineth-inheritance-constructors in child-parent classes-instantiation process of subclasses

Source: Internet
Author: User

One.

The characteristics of the constructor in the child parent class.

For example,

The constructor is not written in the class, and it is there. Just write it out and you can see it more clearly.

The DOS results are very special, and in the case of Zi run, there is the existence of Fu run. According to reason, the object created by the subclass should be the content of the subclass, where the contents of a parent class suddenly appear. In addition, the initialization of the object is only involved in the operation.

Now we discuss why the subclass creates the object constructor initialization, there will be a constructor for the parent class also has initialization, and precedes the subclass.

from the thought raised above , when super () will appear? What was the reason why the super appeared when we talked about the same thing in this class and object?

The real situation is as follows, in the Zi () constructor, there is a super () statement, which can be compared to the previous this () and this () refers to the constructor in this class by using the This keyword with parentheses. In the same way, the Super keyword is used to invoke functions in the parent class, and the super and this keyword are similar.

The correct process is described again: Main in the first new Zi (), in the heap to open up space, the allocation of address values, the default initialization of nothing (because there is no member variable). After the construction is initialized, the Zi () is accessed, and the Zi () enters the memory and the inside begins to execute. There are three sentences in the subclass constructor, Super (); System.out.println ("Zi Run"); return; (Return is hidden). →super (); Is implicit, not written also exists. In such an inherited subclass constructor, super is a certain existence. (?)。 when is return hidden, or where should it exist ?

There is a super () that points to the parent class, and the Fu () constructor is in the stack, so the first thing to do is output fun run. Then output Zi run, and run return.

Why is super calling an empty argument constructor here? The reason is that, in the event that the parent class does not have a write constructor, it is the default constructor, which is a function of an empty argument list. (The explanation is very strange)

Who says the parent constructor must be empty, adding something else inside. The DOS result shows an error. The subclass has super (); The default is a constructor that has null arguments, and the parent class does not have an empty parameter constructor, which fails.

At this point, who says when defining a class, be sure to write the constructor of the null parameter? To write what parameters to write what parameters, how to construct the initialization, how to construct the initialization. This can be specified directly with super. That means that the constructor for an empty argument does not exist when there is a constructor for the argument list .

constructor override no?

Why is there no overlay? Can constructors in subclasses be the same as constructors in a parent class? No way.

Note: Constructors cannot be absent from the child parent class.

As far as inheritance is concerned, subclasses can be a lot of things from the parent class, but the constructor of the parent class is not inherited. The constructor of the parent class can only be called through Super (), which is called the instantiation of the subclass.

The instantiation of a subclass is the creation of an object.

This is the process of initializing it. Is it necessary to initialize a class?

Now the constructors in the subclass are expanded, and there is more than one constructor in the class. This is true, and it is not possible to have only one default constructor in the class.

What was the result? The result of the DOS display is a,d. Here is the emphasis on instantiation of existence. It is important to pay attention to the existence of shadow hide statements, in the actual development of many are not writing super ().

It is also important to emphasize that if there is no empty argument constructor in the parent class (no words, you need to customize an empty parameter constructor), the instantiation of the subclass process will not go on, DOS will error.

An empty parameter constructor and a default constructor. If there is no custom null parameter constructor, then there will be a system default constructor exists, so that the instantiation of the subclass of the process can continue to run.

A

What does this example show? When a constructor parameter with a parameter list is defined in the parent class, the null argument constructor does not exist. An error occurred in the process that caused the subclass to instantiate.

B

The parent class constructor is an empty argument list, and the execution statement inside does not conform.

C

D

This example shows that there is a default constructor for a null argument when the parent class does not have a constructor defined.

Note: does the instantiation of subclasses have to be done ?

java-Preliminary Understanding-nineth-inheritance-constructors in child-parent classes-instantiation process of subclasses

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.