Several uses of super in Java and the difference from this

Source: Internet
Author: User

1. Subclass constructors If you want to refer to super, you must put super in the first place of the function .

1 classBase {2 Base () {3System.out.println ("Base");4     }5 }6  7  Public classChecketextendsBase {8 Checket () {9        Super();//call the constructor of the parent class, and be sure to put it in the first statement of the methodTenSystem.out.println ("Checket"); One     } A   -      Public Static voidMain (String argv[]) { -Checket C =NewChecket (); the     } -}

If you want to use super to inherit the method constructed by the parent class, but not on the first line, then the statement before super is definitely to satisfy the statement that you want to complete some behavior, but also uses super to inherit the constructor of the parent class. The changes that were made before are all back to the previous, which means that the parent class is constructed.

2. In Java, it is sometimes encountered that a member variable or method in a subclass has the same name as a member variable or method in a superclass (sometimes called a parent class). Because a member variable or method name in a subclass has a high precedence, a member variable or method of the same name in the subclass hides the member variable or method of the superclass, but if we want to use this member variable or method in the superclass, we need to use super.

1 classCountry {2 String name;3  4     voidvalue () {5Name = "China";6     }7 }8  9 classCityextendsCountry {Ten String name; One   A     voidvalue () { -Name = "Hefei"; -     Super. value ();//when this method is not called, Super.name returns the value of the member variable of the parent class null the System.out.println (name); -System.out.println (Super. Name); -     } -   +      Public Static voidMain (string[] args) { -City c=NewCity (); + C.value (); A        } at}

In order to refer to the member variables in the parent class in the subclass name and method value (), Super, Super.name, and Super.value () are used in the code, and if Super.value () is not called, the Super.name returns the default value of the parent class member variable null, when this method is called, the Super.value () method assigns the member variable name to China, and then uses Super.name to invoke the value of the member variable of the parent class.

Also, note that the value of the member variable is called by the Super.name.

1 classCountry {2String name= "Xianfan";3  4 String Value (string name) {5Name = "China";6        returnname;7     }8 }9  Ten classCityextendsCountry { One String name; A   - String Value (string name) { -Name = "Hefei"; the Super. Value ("Failed");//when this method is not called, Super.name returns the value of the member variable of the parent class null - System.out.println (name); -System.out.println (Super. Name); -        returnname; +     } -   +      Public Static voidMain (string[] args) { ACity c=NewCity (); atC.value ("Success"); -     } -}

The result is: Hefei

Xianfan

At this point, the value returned by Super.name is the value of the parent class member variable Xianfan, and at this point the Super.value () method is not working.

3. Pass parameters directly with super:

1 classPerson {2      Public Static voidprt (String s) {3 System.out.println (s);4     }5  6 Person () {7PRT ("A person.");8     }9  Ten Person (String name) { OnePRT ("A person name is:" +name); A     } - } -   the  Public classChineseextendsPerson { - Chinese () { -        Super();//calling the parent class constructor (1) -PRT ("A Chinese.");//(4) +     } -   + Chinese (String name) { A        Super(name);//calling a constructor with the same formal parameters as the parent class (2) atPRT ("His name is:" +name); -     } -   -Chinese (String name,intAge ) { -         This(name);//call the constructor that currently has the same formal parameter (3) -PRT ("His age is:" +Age ); in     } -   to      Public Static voidMain (string[] args) { +Chinese CN =NewChinese (); -CN =NewChinese ("Kevin"); theCN =NewChinese ("Kevin", 22); *     } $}

The result is: 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 are no longer used as before. Connect a method or member, but follow the appropriate parameters directly thereafter, so the meaning of it changes. Super is used to invoke constructors that have the same form in the parent class, such as 1 and 2. This is followed by a constructor that currently has the same parameters, such as 3. Of course, in the various overloaded constructors of Chinese, this and super are still available in various usages of the general method, such as 4 where you can replace it with "this.prt" (because it inherits that method from the parent class) or "Super.prt" (because it is a method in the parent class and can be accessed by the class), it works correctly. But it seems to be a little superfluous flavor.

4. The similarities and differences between Super and this:

1) Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)

2) This (parameter): Invokes another form of the constructor in this class (should be the first statement in the constructor)
3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member function name (argument)

4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)

5) call Super () must be written in the first row of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.

6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.

7) Super () and this () must be placed in the first line of the construction method.

8) Although you can call a constructor with this, you cannot call two.

9) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.

This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.

11) In essence, this is a pointer to this object, but super is a Java keyword.

Several uses of super in Java and the difference from this

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.