A simple understanding of super and this in Java

Source: Internet
Author: User

I recently encountered some questions about super when I was studying java. At first I saw that super had no idea. I found some information and studied it:

In Java, sometimes the member variables or methods in the subclass have the same name as the member variables or methods in 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 parent class, however, if we want to use this member variable or method in the superclass, we need to use super.

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 you want to complete certain behaviors, but you can use 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.

In addition, we should talk about super, this first,

1. It can represent the constructor transfer. This (a, B) indicates that another constructor is called. This is a special syntax. It is not a variable and has no type.

2. It can be used inside a non-static member of a class to indicate the current object. At this point, this is a normal final variable, which has a static type, namely the class C itself; it has a dynamic type, which is the type of the current object. You can call a member function on it, pass it to another function, and so on. As long as a final variable of the C type can appear, it can appear.

"Super ". It is similar to "this", but it is also different. It can be used inside a non-static member of a class. For example, super. Method (). "Super" is not a variable because calls to any public non-static function in Java are polymorphism. Another function of super is to call the protected function of the parent class. Sometimes, when constructing a constructor with parameters, to call a base class constructor with parameters, you must use super to explicitly write the constructor statements, in addition, it is configured with an appropriate parameter list.

Below are some similarities and differences between super and this collected on the Internet:

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.