Usage of the super keyword in Java

Source: Internet
Author: User

This article summarizes the usage of super keywords in Java.

There is a reference to the parent class object mentioned in the book. (I have a bit doubt about this statement. If it indicates the reference of the parent class object, it should be said as follows: System. out. println (super); can output the representation of the parent class object.

However, the above sentence output is incorrect, while System. out. println (this); no problem. The following is a doubt. Since super represents the reference of the parent class Object of the current object, its type is naturally the parent class.

For example

The Code is as follows: Copy code

Class {}

Class B extends {

A print (){

Return super;

}

}

At this time, the compiler will report that this method must return the type. Isn't super the type here? For more information, see super's questions.

)

From this point on, we have overturned the phrase that super is the reference of the parent class Object of the current object. It is just a keyword (more powerful evidence, unable to come up, wondering ....)

Super is not a reference address of an object in a heap space.

Isn't that an object reference?

However, it references the features of the parent class.

The parent feature is not an object, so it cannot be called an object reference.

Where can super be used?

1. Used in the constructor: indicates that the parent class is called for constructor.

(1) For example: Parent (int I) {}, Child () {}, then the compiler will assume that we are in the Child () {super ();} add super (). Therefore, either we provide a non-argument constructor for the parent class or explicitly call super (3) in the subclass );

Super calls the constructor. Only the first sentence in the constructor can be called.

2. used in non-static methods: variables or methods of the parent class can be called (non-private)

Super calls the methods and variables of the parent class object, which has special effects like this.

The Code is as follows: Copy code
Class Whuedu {
{
String schoolName;
String address;
Edu (String s, String add)
{
SchoolName = s;
Address = add;
PrintfEdu ();
}
Public void printEdu ()
{
// Super calls the method of the parent class
Super. printEdu ();
}
}

Generally, you do not like to call the member variables of the parent class.


Use super to call the constructor of the parent class
Subclass does not inherit the constructor of the parent class. Therefore, if a subclass wants to use the constructor of the parent class, it must be used in the constructor of the subclass and must be represented by the keyword "super. In addition, super must be the first statement in the subclass constructor. Example:
Example 24:
 

The Code is as follows: Copy code
Class Student
{
Int number; String name;
Student (int number, String name)
{
This. number = number; this. name = name;
System. out. println ("I am" + name + "my number is" + number );
}
}
Class Univer_Student extends Student
{
Boolean marital status;
Univer_Student (int number, String name, boolean B)
{
Super (number, name );
Marital status = B;
System. out. println ("Marital status =" + marital status );
}
}
Public class Example4_24
{
Public static void main (String args [])
{
Univer_Student zhang = new Univer_Student (9901, "And Xiaolin", false );
}
}
Running result
I am and Xiaolin my number is 9901
Marital status = false.

Note: If the super keyword is not displayed in the constructor of the subclass to call a constructor of the parent class
Super ();
Statement, that is, the constructor that calls the parent class without parameters. If the parent class does not provide a constructor without parameters, an error occurs.
2. Use super to operate hidden member variables and Methods
If we want to use the member variables or methods of the parent class hidden by the quilt class in the child, we can use the keyword super. For example, super. x and super. play () are member variables x and play () of the parent class hidden by the quilt class ().
Example 25:

The Code is as follows: Copy code
Class Sum
{
Int n;
Float f ()
{
Float sum = 0;
For (int I = 1; I <= n; I ++)
Sum = sum + I;
Return sum;
}
}
Class Average extends Sum
{
Int n;
Float f ()
{
Float c;
Super. n = n;
C = super. f ();
Return c/n;
}
Float g ()
{
Float c;
C = super. f ();
Return c/2;
}
}
Public class Example4_25
{
Public static void main (String args [])
{
Average aver = new Average ();
Aver. n = 100;
Float result_1 = aver. f ();
Float result_2 = aver. g ();
System. out. println ("result_1 =" + result_1 );
System. out. println ("result_2 =" + result_2 );
}
}
Running result
Result__1 = 50.50
Result_2 = 2525.0

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.