When to call super.nameAll when override(override) a method?

Asked

Viewed 324 times

3

In one class we have several methods superimposed with the @Override, and in some, for example onPreExecute of an extended class of AsyncTask, comes with the super.onPreExecute().

@Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

What would that method be super.nomeDoMetodo() and whether or not to leave this call to the upper-class method?

  • I edited the question to make it more objective, see if that’s it.

  • Just lacked "what would be" this super method that some have and others do not, and yes when to leave it or not. ;)

  • Add it, good thing you already have 2 responses and they update. :)

  • 2

    The method super is used to execute the code of the "parent" method. Sometimes it is necessary to leave (see the code of activities). But when there is no mandatory code in the "parent" method, this line becomes unnecessary.

2 answers

7


When writing a class extending from another it is possible to overwrite your methods.

In doing so you are writing another implementation, the original implementation being discarded/overwritten.

However, there are cases that, due to the way the class has been implemented, it is necessary that the original implementation of the superscript method is executed.

The way to do it is to use super.nomeMetodoSobrescrito().

When it is an abstract method it is never necessary to do so.
In other cases it shall do so when the class documentation so indicates.

As far as Android SDK is concerned, at least in the use of the Activity class, an exception will be made if you do not.
If, in Android Studio, use Generate... -> Override Methods... the method call will be automatically placed when needed.

  • I think as follows: I superimposed the method with the @Override, but I’m still bringing all the contents of it with the super.nomeMetodo() and implementing with my need, correct !?

  • Yes that’s what it is.

5

Asynctask is an abstract class with some methods whose default implementation does nothing, including onPreExecute().

These methods should be superscripted (implemented) by the Asynctask subclasses.

That is, there is no standard implementation of onPreExecute(). There is no need to call super.onPreExecute() as this call will do nothing. This applies to some Asynctask methods.

There are situations where calling the super makes sense. This is (or at least should be) documented in the method or class Javadoc. For example, when overwriting Activity.onCreate(). In this case there is already a standard implementation in the superclass that must be supplemented with the superscript method in the subclass.

"super" is a reference to the superclass. Just as "this" is a reference to the object itself. Calling method() is the same as calling this.method(), and calling super.method() will invoke the "parent" version of the method, i.e., the implementation done in the superclass.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.