The Java Foundation Super keyword

Source: Internet
Author: User

one, in Java, there are usually two ways to use the Super keyword:

1. In the subclass of the constructor (initialization), the main call is the default constructor of the parent class, if the parent class has more than one construction method, you can specify a specific constructor by super, such as Super (paras);

2. Use a subclass to invoke a hidden or overridden property or behavior , such as Super.ondestroy (), and so on;

For the 1th thing to note, super represents the parent class of the current class, and super () calls the parent class's default constructor, which allows the parent class to be initialized. How the parent class is not initialized, when the subclass calls the method of the parent class, is logically wrong, because the parent class is not initialized, and the methods and properties of the parent class have no memory space.

Second, about the super and this keyword comparison (difference):

1. Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
2. This (parameter): Calls 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, if the function's shape participates in the class the member data has the same name, then need this to indicate the member variable name)
5. Call super () must be written on the first line 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 () and this () are similar, except that super () calls the constructor of the parent class from the subclass, and this () calls other methods within the same class.
7. Super () and this () need to be placed in the first line within 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.
Both this () and super () 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.

third, for Android Super.ondestroy (); Should I put it in front or behind? Why?

* * Style 1 * *:

@Override public void onDestroy() { // TODO: some code sUper.ondestroy (); } 

* * Style 2 * *:

@Override
public void OnDestroy () {
Super.ondestroy ();
Todo:some Code
}

Many people will be troubled, which method is better? On the one hand many people will support to put super on the first line, on the other hand many people are afraid of the interface destroyed after the execution may throw a null pointer. So which one is the right order?

In the Android source code, most of the source code tends to style one, such as: android.app.ListFragment and android.app.ListActivity , and android.speech.RecognitionService so on (API 25).

Android.app.ListFragment:

/**
* @see Activity#ondestroy ()
*/
@Override

protected void OnDestroy () {
Mhandler.removecallbacks (Mrequestfocus);
Super.ondestroy ();
}

Does the Super method have to be placed on the first line? The official example shows that this is not the case. A lot of people would support putting super on the first line, stating that the fundamentals are not solid enough. In Java syntax It is indeed in the subclass constructor that super () must be placed on the first line, with the constructor in mind, and the parent class object to be constructed first.

the parent method has no such requirement . See OnDestroy source code, you can see that in super () after the write operation may cause the operation of the object burst NullPointerException; (only one line of code of the probability is still very low, but theoretically exist)

Reference Links:

80405563

The Java Foundation Super 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.