The difference between the super () and this () in the Java Face question summary

Source: Internet
Author: User

What is the difference between super () and this ()? This () invokes the parameterless constructor of the current object, and super () calls the parameterless constructor of the parent class
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.

The difference between the super () and this () in the Java Face question summary

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.