How to find out when / where to call an overridden superclass method

This question arose in me when programming an Android application, but it seems to be a general programming question.

The situation is that I am extending (subclassing) the class from the library and redefining the method. How do I know if I should use the superclass method? and when? (at the beginning of an overridden method or at the end?)

For example, I override the method "public boolean onCreateOptionsMenu (Menu)" from the "Activity" class on the Android platform. And I saw that someone wrote "return super.onCreateOptionsMenu (menu)" at the end of the method, in the example. But how do I know that this should be done? and is it right or wrong? what's the difference if i start my method with "super.onCreateOptionsMenu (menu)"?

BR, Henry

+4
source share
6 answers

I do not think that you can answer this question in an abstract: it depends on the behavior of the superclass that you redefine.

Depending on the circumstances, it may be appropriate:

  • call super first
  • super last call
  • handle some cases yourself (settings), the call is super for the rest
  • never call super

Let's hope that the documentation for a specific class that you redefine will tell you when / when to call super.

+6
source

Unfortunately, there is no rule for this. You need to access the API documents and call super if you need documents.

One hint as to whether you need it in the case of Android or not is that the method you override is one of the life cycle methods. In this case, you can be pretty sure that you need to call super.

+1
source

These are valid questions, but unfortunately there is no general rule.

Regardless of whether you need a super method call, it depends on whether the super method really does something that needs to be done. In other words: do you extend or replace an overridden method? Good class API documentation should give you an answer. In addition, libraries often follow certain conventions to make it clear how to use them.

The answer to the question of where to place the super call depends on when you want to run the extension youre. Do I need to run before or after the super method? Most often, you first call super, and then do something extra. But if you need to prepare something for a super method, for example, to change some state of an object or manipulate arguments, you will place the code before the super call. Again, the API documentation should give you the answer here.

+1
source

Android in most cases throws an exception if you forget to call super. I would believe that you do not need to call super if you do not, and it does not give up.

In discussions with google groups, some best practices for lifecycle methods have been developed (they are not officially supported by data, but are used by many programmers):

  • If you use the method of creating onCreate, onResume, etc. call super as the first statement. Thus, you can be sure that everything that needs to be prepared from the superclass is prepared.
  • If you use a closure method like onSaveInstanceState, onPause call super last. Now you can be sure that nothing will be removed or changed to a bad state before you do everything.
+1
source

You do not need to call super.method (), you only call it when you need it, where you need it.

0
source

When overriding a method in a child class, it depends on the type of instance you are invoking this method on.

eg:

Class Animal{ public eat(){ //.. } } Class Dog extends Animal{ public eat(){ //.. } } now if you say new Dog().eat() it executes Dog eat method. if you say new Animal().eat() it executes Animal eat method. 

And you might have code like this

  Animal a = new Dog(); a.eat(); 

which executes the Dog eat method again, since the actual instance is of type Dog.

0
source

Source: https://habr.com/ru/post/1306905/


All Articles