Super and this use problem at the same time

Source: Internet
Author: User

We all know that this and super call constructors must be placed in the first sentence, and today the classmate asked me a question of a little meaning.

So: How do I explicitly initialize a parent class with super in a subclass and initialize the subclass with this?

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

First of all, we must realize that two points:

1. The super call constructor functions to initialize the parent class before initializing the subclass, that's all.

2. The constructor for each class has and only one initialization that performs the property, even if you use this, which is actually the constructor that the this point is going to execute.

Under the following two circumstances specific analysis

(1) The parent class provides a parameterless constructor

This is easier because the constructor of the subclass automatically calls the parent class's parameterless construction method, and does not require an explicit addition of super ().

But what if my hands are cheap and hard to add? It will not compile, otherwise it will initialize the parent class more than once, and there is a potential risk of inconsistent state.

For example, the parent class provides a parameterless constructor, and the subclass has a default constructor, three parameters with a constructor.

As stated above, when instantiated, there is only one constructor that executes, and when Son1 () is used, son1 (int p3) eventually calls the last constructor,

So the constructor of the parent class is called before the last constructor executes, and if Son1 (), son1 (int p3) also shows the call Super (), the parent class is initialized more than once.

This is obviously not true, and son1 (int p2, int p3) Initializes the property itself, so there is no problem, you can explicitly call super ().

 Public classFather {intAge ;  Publicfather () { age= 50; }} Public classSon1extendsfather{intproperty1; intProperty2; intProperty3;  PublicSon1 () { This(A); }         PublicSon1 (intp3) {         This(, p3); }         PublicSon1 (intP2,intp3) {        Super(); Property1= 1;  This. Property2 =P2;  This. Property3 =P3; }         PublicSon1 (intP1,intP2,intp3) {        Super(); Property1=P1; Property2=P2; Property3=P3; }}

(2) The parent class does not provide an argument-free constructor

In this case, you must explicitly call Super (PARM) to initialize the parent class in the child class.

Similarly, the following two constructors must explicitly call the super

 Public classFather {intAge ;  PublicFather (intAge ) {         This. Age =Age ; }} Public classSonextendsfather{intproperty1; intProperty2; intProperty3; //Constructor 1    PublicSon () {//super (+);        This(A); }   //Constructor 2    PublicSon (intp3) {       //super (+);        This(, p3); }  //Constructor 3    PublicSon (intP2,intp3) {       Super(40); Property1= 1;  This. Property2 =P2;  This. Property3 =P3; }  //Constructor 4    PublicSon (intP1,intP2,intp3) {       Super(40); Property1=P1; Property2=P2; Property3=P3; }} 

Summary: This and super call constructors must have the first row so that they cannot be used at the same time, but you do not need to worry about not initializing the parent class.

Because, this finally points to the subclass constructor, it is bound to call super () to initialize the parent class, either by default or with a parameter.

Super and this use problem at the same time

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.