Use of Java super (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/26/2789722.html

Access members of the parent class

If you overwrite the methods of the parent class, you can use super to call the methods covered by the parent class, you can also use super to reference hidden variables (although hidden variables are not recommended ).

Assume that the parent class is like this:

 
Public ClassSuperclass {Public VoidPrintmethod () {system. Out. println ("Printed in superclass .");}}

Here is a subclass, which will overwrite the printmethod () method ():

  Public   class  subclass  extends   superclass { ///   overrides printmethod in superclass   Public   void   printmethod () {  super  . printmethod (); system. out. println ( "printed in subclass" ) ;}  Public   static   void   main (string [] ARGs) {subclass S  =  New   subclass (); S. printmethod () ;} 

The printmethod () method of subclass overwrites the printmethod () method of the parent class. to reference the printmethod () method of the parent class, a modified name must be used, use super. compile and execute subclass. The output is as follows:

 
Printed in superclass. Printed in subclass

 

Subclass Construction Method

The following example illustrates how to use the super keyword to call the constructor of the parent class. In the previous example of the bicycle, mountainbike is a subclass of the bicycle, where mountainbike (subclass) the constructor calls the parent constructor and adds its own initialization.CodeExample:

PublicMountainbike (IntStartheight,IntStartcadence,IntStartspeed,IntStartgear ){Super(Startcadence, startspeed, startgear); seatheight=Startheight ;}

The constructor that calls the parent class must be located in the first line of the constructor of the subclass.

The syntax for calling the constructor of the parent class is as follows:

 
Super(); Or:Super(Parameter list );

Super () is the non-parameter constructor that calls the parent class, and super (parameter list) is the constructor that calls the parameters corresponding to the parent class.

Note: If the subclass does not explicitly call the constructor of the parent class, the Java compiler will automatically insert an instruction that calls the constructor without parameters of the parent class. If the parent class does not have a constructor, an exception occurs during compilation. the object does not have this problem. If the object is a unique parent class, no problem exists.

If the constructor of the subclass calls the constructor of the parent class, whether explicit or implicit, you may think about how the constructor passes through the links of all objects. actually, it exists. It is called the constructor chain, and you must realize that there is an identical link returned from the top class.

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.