The usage of super in Java and the difference with this

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/anmei2010/article/details/4093118

1. Sub-class constructor if you want to reference super, you must put super in the first place of the function.

Class base {base () {system. out. println ("base") ;}} publicclass checketextends base {checket () {super (); // call the constructor of the parent class, which must be placed in the first statement system of the method. out. println ("checket");} publicstaticvoid main (string argv []) {checket c = new checket ();}}

 

If you want to use super to inherit the method constructed by the parent class, but it is not placed in the first line, then the statement before Super must be used to satisfy the statement that you want to complete certain behaviors, however, super inherits the constructor of the parent class. Then all the previous modifications will go back to the previous one, that is, they will become the constructor of the parent class.

2. in Java, sometimes the member variables or methods in the subclass are the same as the member variables or methods in the super class (also known as the parent class. Because the member variables or method names in the subclass have a high priority, the member variables or methods with the same name in the subclass hide the member variables or methods of the superclass, however, if we want to use this member variable or method in the superclass, we need to use super.

Class country {string name; void value () {name = "China" ;}} class cityextends country {string name; void value () {name = "Hefei"; super. value (); // when this method is not called, super. name returns the value of the member variable of the parent class nullsystem. out. println (name); system. out. println (super. name);} publicstaticvoid main (string [] ARGs) {City C = New City (); C. value ();}}

 

To reference the member variable name and method value () in the parent class in the subclass, super and super are used in the code. name and super. value (). If super. value (), super. name returns the default value of the parent class member variable null. When this method is called, super. the value () method assigns the member variable name to China, and then uses super. name indicates the value of the member variable of the parent class.

In addition, note that super. Name calls the value of the member variable,

Class country {string name = "xianfan"; string value (string name) {name = "China"; return name ;}} class cityextends country {string name; string Value (string name) {name = "Hefei"; super. value ("failed"); // when this method is not called, super. name returns the value of the member variable of the parent class nullsystem. out. println (name); system. out. println (super. name); Return name;} publicstaticvoid main (string [] ARGs) {City C = New City (); C. value ("successful ");}}

 

Result: Hefei

Xianfan

At this time, the value returned by Super. Name is the value of the parent class member variable xianfan, and the super. Value () method does not work at this time.

3. Use super to directly pass parameters:

Class person {publicstaticvoid PRT (string s) {system. out. println (s);} person () {PRT ("a person. ");} person (string name) {PRT (" A person name is: "+ name) ;}} publicclass chineseextends person {Chinese () {super (); // call the parent Constructor (1) PRT ("a Chinese. "); // (4)} Chinese (string name) {super (name); // call the constructor with the same parameters of the parent class (2) PRT ("His name is:" + name);} Chinese (string name, int age) {This (name); // call a constructor with the same parameters currently (3) PRT ("his age is:" + age);} publicstaticvoid main (string [] ARGs) {Chinese Cn = new Chinese (); Cn = new Chinese ("Kevin "); CN = new Chinese ("Kevin", 22 );}}

 

Result: a person.

A Chinese.

A person name is: Kevin

His name is: Kevin

A person name is: Kevin

His name is: Kevin

His age is: 22

In this program, this and super no longer use ". "connects a method or member, but directly follows the appropriate parameter, so its meaning changes. Super is used to call constructors with the same form in the parent class, such as 1 and 2. If this is followed by a parameter, the constructor with the same parameter is called, for example, 3. Of course, in each overload constructor in Chinese, the usage of this and super in general methods can still be used. For example, you can replace it with "This. PRT (because it inherits the method in the parent class) or "super. PRT (because it is a method in the parent class and can be accessed by the quilt class), it can still run correctly. However, it seems a bit fascinating.

4. Similarities and Differences between super and this:

1) Super (parameter): Call a constructor in the base class (it should be the first statement in the constructor)

2) This (parameter): Calls another constructor in this class (it should be the first statement in the constructor)
3) Super: it refers to the members of the direct parent class of the current object (used to access member data or functions of the hidden parent class in the direct parent class, when the base class and the derived class have the same member definition, such as: Super. variable name super. member letter data name (real parameter)

4) This: it represents the name of the current object (this should be used to indicate the current object when it is prone to ambiguity in the program; if the member data in the function form participation class has the same name, this is required to specify the member variable name)

5) Calling super () must be written in the first line of the subclass constructor; otherwise, the compilation fails. The first statement of each subclass constructor method implicitly calls super (). If the parent class does not have such constructor, an error is reported during compilation.

6) Super () is similar to this (). The difference is that super () calls the constructor of the parent class from the subclass. This () calls other methods in the same class.

7) both super () and this () must be placed in the first line of the constructor.

8) although you can use this to call a constructor, you cannot call two constructor.

9) This and super cannot appear in a constructor at the same time, because this will inevitably call other constructor, and other constructor will inevitably have super statements, therefore, if the same statement exists in the same constructor, the meaning of the statement is lost and the compiler will not pass.

10) This () and super () both refer to objects. Therefore, they cannot be used in the static environment. Including static variables, static methods, and static statement blocks.

11) in essence, this is a pointer to the current object, but super is a Java keyword.

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.