Similarities and differences between Java super and this

Source: Internet
Author: User

Similarities and differences between Super and this: super (parameter): Call one of the constructors in the base class (which should be the first statement in the constructor) this (argument): Call another constructor in this class (which should be the first statement in the constructor) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in a parent class that is hidden in the immediate parent class. When a base class has the same member definition as a derived class: Super. Variable name super. member function name (argument) This: it represents the current object name (where two of the semantics are easily generated in a program, You should use this to indicate the current object, and if the member data in the function's shape-participating class has the same name, you need this to indicate the member variable name. The call super () must be written in the first line of the subclass constructor, otherwise the compilation does not pass. The first statement of each subclass construction method implicitly calls Super (), and if the parent class does not have this form of constructor, the error occurs at compile time. Super () is similar to this (), except that super () calls the constructor method of the parent class from the subclass, this () calls other methods within the same class. The 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 cannot appear in a constructor at the same time, because this is bound to invoke other constructors, and other constructors must have the presence of a super statement, so the same statement in the same constructor loses the meaning of the statement and the compiler does 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. In essence, this is a pointer to this object, but super is a Java keyword.

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.