Several uses of super in Java, 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. class Base {base () {System.out.println ("base"); }} public class Checket extends Base {checket () {super ();//Call the constructor of the parent class, be sure to place the first statement of the method System.out.println ("Che    Cket ");    } public static void Main (String argv[]) {checket c = new Checket (); }} 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.     Class Country {String name;    void value () {name = "China";     }} class City extends 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, NULL System.out.println (name);    System.out.println (Super.name);       } public static void Main (string[] args) {City c=new city ();       C.value (); }} In order to refer to the parent class in the child classThe member variable name and method value (), the Super, Super.name, and Super.value () are used in the code, and if Super.value () is not called, 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 Super.name calls the value of the member variable, class Country {String name= "Xianfan";       String value (string name) {name = "China";    return name;     }} class City extends 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 null SYSTEM.OUT.PRI       NTLN (name);       System.out.println (Super.name);    return name;       } public static void Main (string[] args) {City c=new city ();    C.value ("Success"); The result is: Hefeixianfan at this point, the value returned by Super.name is Xianfan the value of the parent class member variable, and the Super.value () method at this time is not working. 3.    Pass parameters directly with super: class Person {public static void prt (String s) {System.out.println (s);    } person () {prt ("A person.");    } person (String name) {prt (' A person name ' is: "+ name); }} public class Chinese extends PeRson {Chinese () {super ();//Call the parent class constructor (1) PRT ("A Chinese.");    /(4)} Chinese (String name) {super (name);//Call the parent class with the same formal parameter as the constructor (2) PRT ("His name is:" + name);    } Chinese (String name, int age) {This (name);//Call the constructor with the same formal parameter (3) PRT ("His age was:" + age);       } public static void Main (string[] args) {Chinese cn = new Chinese ();       cn = New Chinese ("Kevin");    cn = New Chinese ("Kevin", 22); The result is: A person.a Chinese. A person name Is:kevinhis name Is:kevina person name Is:kevinhis name Is:kevinhis age is:22 in this program, this and super are no longer like Used to do "." 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. 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): Call another constructor in this class (which should be the first statement in the constructor) 3) Sup Er: It refers to a member in the immediate parent class of the current object (used to access member data or functions in the parent class that is hidden in the immediate parent class, as in the case of the same member definition in the derived class, such as: Super. Variable name super. member function by name (argument) 4) This: it represents the current object name (ambiguity , you should use this to indicate the current object, or if the member data in the function's form participates in a class with the same name, this is required to indicate the member variable name) 5) calling super () must be written in the first row of the subclass construction method, otherwise the compilation will 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, the difference from this

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.