2

While overriding methods like onPostExecute from AsyncTask we can avoid or remove super.onPostExecute(result); from it. Can you tell me why and when do we need to use this call? as I here implemented AsyncTask as a inner class in my Main Activity

    private class CustomAsyncTask extends AsyncTask<String, Void, Event> {
    @Override
    protected Event doInBackground(String... strings) {
        Event result = Utils.fetchEarthquakeData(strings[0]);
        return result;
    }

    @Override
    protected void onPostExecute(Event result) {
        super.onPostExecute(result);// what is the effect of calling this? If it was possible to skip or remove it. 
        updateUi(result);
    }
    
}
Sarwar Sateer
  • 395
  • 3
  • 16
  • 1
    For one, it helps to keep the code [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). – Turing85 Aug 15 '20 at 10:47
  • in the method above We can skip that and call that method. is there any effect of calling super class's method over there? – Sarwar Sateer Aug 15 '20 at 10:54
  • 1
    Without seeing the code, it is hard to tell. I suspect it to have some effect, otherwise the code would be pointless. – Turing85 Aug 15 '20 at 10:56
  • 1
    Usually, you want to keep the original behavior, as specified by the framework. The API documentation will tell you if you have to do it. To know why, you would have to delve into the source code of the framework and understand the underlying architecture. – Tarik Aug 15 '20 at 11:00
  • A *requirement* to call super [is considered an antipattern](https://stackoverflow.com/q/13994150/3788176). – Andy Turner Aug 15 '20 at 11:03

3 Answers3

3

When you override a method, you redefine the method i.e. if this method is called on the instance of the child class (in which you have overridden the method), this new version of the definition will be called.

When you override a method (i.e. redefine or in other words, replace the superclass definition with the child's version of the definition), you have two choices:

  1. Do not use anything from the superclass definition and rewrite it in a completely new way.
  2. Reuse the superclass definition at some point (in the beginning/middle/end) in the new definition. It's like putting a cherry on the ice cream cone where ice cream cone is the superclass definition of the method and cherry is added in the new definition.

Note that as already pointed out by Andy Turner, a requirement to call super is considered an antipattern.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

when we override some method then it means:

  1. we have interface in our project.
  2. In that interface One Class is Parent and Other one is Child.

Now Overriding means having same method in both Parent and Child. Se below example:

class Parent
{

 public Parent()
 {
  "Inside Parent Constructor";
 }
 
 public void meth()
 {
  "Inside Parent Method";
 }

}

class Child
{

 public Child()
 {
  "Inside Child Constructor";
 }
 
 public void meth()
 {
  super();              
  "Inside Parent Method";
 }

}

Due to super keyword in Child Class it will go to Constructor of Parent. and Print "Inside Parent Constructor".

So main thing is that many of the time Andoid OS will want to access Parent Class from which the method is overrided and for that we write super().

Dharman
  • 30,962
  • 25
  • 85
  • 135
SaiAbhijit
  • 386
  • 4
  • 6
2

What is the purpose of calling super class's method while overriding a method?

The purpose is to run the method in the class from which the current class inherits from to perform some work that class is responsible, as it could be some setup of the super class' state.

For your particular case, there is no need to call super.onPostExcecute() as stated in the question below:

Should I call super.onPostExecute(result) in Android AsyncTask?

Juan
  • 5,525
  • 2
  • 15
  • 26