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.