Summary of usage of this and super in Java

Source: Internet
Author: User

These days to see the class in the inheritance will use this and super, here do a little summary, with you to communicate, there are errors please correct me ~

This

This is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself.

The usage of this can be broadly divided into 3 types in Java:

1. Normal Direct references

This does not have to be said, this is equivalent to pointing to the current object itself.

2. The name of the member who participates in the membership is distinguished by this:

Class Person {     private int: age = ten;    Public person () {    System.out.println ("Initialization Age:" +age);}    public int Getage (int.) {        this.age = age;        return this.age;    } } public class Test1 {public     static void Main (string[] args) {person         Harry = new Person ();        System.out.println ("Harry's Age is" +harry.getage);}    }        

Operation Result:

Age of initialization: 10
Harry ' s age is 12

As you can see, age here is the formal parameter of the Getage member method, and This.age is a member variable of the person class.

3. Reference constructors

Let's talk about this with super, see below.

Super

Super can be understood as a pointer to a super (parent) class object, which refers to a parent class closest to itself.

There are three ways to use super:

1. Normal Direct references

Like this, super is the parent class that points to the current object, so you can refer to the members of the parent class with Super.xxx.

2. The member variable or method in the subclass has the same name as a member variable or method in the parent class

Class Country {    String name;    void value () {       name = ' China ';    }} class City extends country {    String name;    void value () {    name = "Shanghai";    Super.value ();//Call Method    System.out.println (name) of the parent class;    System.out.println (Super.name);    }     public static void Main (string[] args) {City       c=new city ();       C.value ();       }}

Operation Result:

Shanghai
China

As you can see, both the methods of the parent class are called and the variables of the parent class are called. If you do not call the parent class method value () and only call the parent class variable name, the parent class name value is the default value null.

3. Reference constructors

Super (parameter): Invokes one of the constructors in the parent class (which should be the first statement in the constructor). this (parameter): Call the constructor of another form in this class (should be the first statement in the constructor).
class person {public static void prt (String s) {System.out.println (s);      } person () {PRT ("parent class • Parameterless constructor method:" + "A person.");      }//construction Method (1) person (String name) {PRT ("parent class • Constructor with one parameter:" + "a person's name is" + name); }//constructor Method (2)} public class Chinese extends person {Chinese () {super ();//Call the Parent class constructor method (1) PRT ("Subclass/Tune         Use the parent class "parameterless construction Method": "+" A Chinese coder. "); Chinese (String name) {super (name);//Call the parent class with the same formal parameter construction method (2) PRT ("Subclass • Call Parent class" constructor with one parameter ":" + "his name is" + Name      ); } Chinese (String name, int age) {This (name);//Call a construction method with the same formal parameter (3) PRT ("subclass: the constructor of the calling subclass with the same formal parameters: his age I      S "+ age);         } public static void Main (string[] args) {Chinese cn = new Chinese ();         cn = new Chinese ("Codersai");      cn = new Chinese ("Codersai", 18); }  }  

Operation Result:

Parent class • No parameter construction method: A person.
Subclass • Call the parent class "parameterless construction method": A Chinese coder.
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Subclass: Calling subclasses to construct methods with the same formal parameters: His-age is 18

As you can see from this example, you can use Super and this to call the constructor of the parent class and other forms of construction methods in this class, respectively.

In the example Chinese class, the third constructor method calls the second construction method in this class, and the second constructor calls the parent class, so the constructor of the parent class is called first, the second in this class is called, and the third method of construction is overridden.

The similarities and differences between Super and this:

  • Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
  • This (parameter): Calls another form of the constructor in this class (should be the first statement in the constructor)
  • Super: 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, when the base class has the same member definition as in the derived class, such as super. Variable name super. member function by name (argument)
  • This: it represents the current object name (where it is easy to generate two semantics in the program, you should use this to indicate the current object, if the function's shape participates in the class member data has the same name, this should be used to indicate the member variable name)
  • The call to super () must be written on the first line 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.
  • 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.
  • Super () and this () all need to be placed in the first row within the construction method.
  • Although you can call a constructor with this, you cannot call two.
  • 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.
  • Essentially, this is a pointer to this object, but super is a Java keyword.

Summary of usage of this and super in Java

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.