Summary of the usage of "Java Learning Note 19" Super in Java inheritance

Source: Internet
Author: User

1) Someone has written a good initialization property constructor, and you just want to add some of your own new properties to the initialization, so that in one constructor to call another constructor, you can avoid duplication of code, reduce the amount of work;

2) When you call another constructor in one constructor, you should use the same memory space, initialize the variable in the default constructor, and override the value of the initialized variable when calling another.

3) The entire invocation of the process and recursive call function is a bit similar, keep inflating the ball, until the whole balloon expands, continuous deep progression, encounter stop mark, layer by hop out.

Wrote a piece of code explaining my narrative above:

1 classJavantiger {2     intAge//Age3     intHight//Body Height4 5      PublicJavantiger () {6 print ();7          This. age=2;//The value of age is initialized here, but the value is overwritten when the recursion returns.8     }9     Ten      PublicJavantiger (intAge ) { One          This();//Call your own first constructor, the number of two statements below does not perform the A          This. Age =Age ; - print (); -     } the  -      PublicJavantiger (intAgeinthight) { -          This(age);//Call yourself the second constructor, the number of two statements below does not execute the -          This. hight =hight; + print (); -     } +  A      Public voidPrint () {//Print function atSystem.out.println ("I am a" + Age + "years" + hight + "ruler Gao Tiger!"); -     } -      Public Static voidMain (string[] args) { -         NewJavantiger (3,3); -     } - } in //Output - //I ' am a 0 years old 0 feet high tiger! to //I ' am a 3 years old 0 feet high tiger! + //I ' am a 3 years old 3 feet high tiger!

Personal understanding is this, there may be problems, such as the constructor recursive call and the program function recursive invocation of the mechanism seems to be the same? The constructor is generated with the object, that is, the memory space is allocated at the same time, will the recursive call such as this will disrupt the order of allocating memory?

Today to summarize is the use of super this keyword, super in the constructor is usually the time when the class inherits the other classes, super is to call the parent class's constructor, the paste segment code First

1 classTiger {2     intAge//Age3     intHight//Body Height4 5      PublicTiger () {6 print ();7     }8 9      Public voidprint () {TenSystem.out.println ("I am a" + Age + "years" + hight + "ruler Gao Tiger!"); One     } A } -  Public classJavantigerextendsTiger { -      PublicJavantiger () { the         Super();//calling a constructor with no arguments to the parent class -     } -      Public Static voidMain (string[] args) { -         NewJavantiger (); +     } -}

In fact, the super () in the constructor of class Javantiger can not write, Java will default to call the parent class parameterless constructor, but if the parent class does not define a parameterless constructor, there is no syntax error, the program will automatically exit, there is no print statement, At this point you need to manually call the constructor of the other parent class, paste the snippet code:

1 classTiger {2     intAge//Age3     intHight//Body Height4 5      PublicTiger (intAge ) {6      This. Age =Age ;7 print ();8     }9      Public voidprint () {TenSystem.out.println ("I am a" + Age + "years" + hight + "ruler Gao Tiger!"); One     } A } -  Public classJavantigerextendsTiger { -      PublicJavantiger () { the         Super(1);//calling a constructor with a parent class with arguments -     } -      Public Static voidMain (string[] args) { -         NewJavantiger (); +     } -}

The super (1) in this code must be written in, or the compiler will make an error. So I simply summed up, "this () is calling its own other constructors, super () is called the constructor of the parent class of its own inheritance", if only to invoke the default parameterless parent class constructor, do not write in the subclass of the constructor function, but the actual programming, always ignore this point.

Can the two of these keywords appear in a constructor of a subclass at the same time? The answer must be no. Let's say your own understanding:

1) When creating a new base class, regardless of the recursive invocation of its own constructor, eventually you will call the parent class constructor, (not explicitly called, the system will call the default parameterless parent constructor);

2) JAVA specifies that this and super must be placed in the first row of the constructor, with only one first row;

Summary of the usage of "Java Learning Note 19" Super in Java inheritance

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.