简体   繁体   中英

What does super() method do?

What does the super method do?

public DataFetch(Context context) {
    super();
    this.ctx = context;
}

Does this constructor make the context of the newly created object the context of the super class? Not 100% sure how this works. So would the super() method essentially just be saying "put me into super class mode" to say it in lay man's terms?

It says "initialize my parent class before you initialize me" by calling its default constructor.

super() calls the parent's class constructor (all the way back to Object) and it runs before the current class's constructor.

There is no such thing as "superclass context" the context is an interface to access information from the application environment like image resources, system services ect. The context will be from whatever you pass in and is class independent. For instance, an Activity is the implementation of the Context interface you are likely to be using and any view you make from within that activity will have the same Context which is actually that provided by the activity.

When used in a constructor, the super() keyword appears alone and must be used before the this keyword can be used. The this keyword may then be used to call functions on a parent object.

My answer may be easy to understand!!!
Example:Look at the syntax:

public class A extends B{  
    //Coding      
}  
public A(){  
    super();  
}

-->Here the super() method requests to first initialize the parent class(ie B) and then the child class(ie A) constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM