tags:

views:

135

answers:

6

Hi,

This question occured to me while programming a Android application, but it seems to be a general programming question more.

The situation is, I am extending (subclass-ing) an class from a library, and overriding a method. how do I know if I should invoke the method of super-class? and when? (in the beginning of the overridden method or in the end?)

For example, I am overriding the method "public boolean onCreateOptionsMenu(Menu menu)" from class "Activity" in Android platform. And I saw someone write "return super.onCreateOptionsMenu(menu)" in the end of the method, in an example. But how do I know it should be done this way? and it is correct or not? what's the difference if I begin my method with "super.onCreateOptionsMenu(menu)"?

BR, Henry

A: 

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

Bakkal
A: 

When you override a method in a child class then it depends on the type of the instance on which you are calling that method.

for example:

   Class Animal{
        public eat(){
           //..
        }
   }

   Class Dog extends Animal{
        public eat(){
           //..
        }
  }

now if you say new Dog().eat() 
it executes Dog's eat method.

if you say new Animal().eat()
it executes Animal's eat method.

And you may have code like this

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

which again executes the Dog's eat method as the actual instance is of the type Dog.

GK
+1  A: 

Unfortunately, there is no rule for this. You need to refer to the API docs and call super if the docs say you need to.

One hint as to whether you'll probably need to or not in Android's case is if the method you're overriding is one of the lifecycle methods. In this case, you can be fairly certain that you need to call super.

JRL
+4  A: 

I don't think you can answer this question in the abstract: it depends on the behavior of the superclass method you're overriding.

Depending on circumstances, it may be appropriate to:

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

Hopefully the documentation for the particular class you're overriding will tell you if/when it's necessary to call super.

David Gelhar
In addition to docs, some common sense and experience will guide you here too. For example, are you trying to do something in addition to what normally happens (do call superclass method) or are you trying to do something instead of the normal behavior (don't)? Does your stuff need to happen first or can it happen at the end.
Bert F
+1  A: 

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

Whether or not you need to call super method depends on the fact if the super method does something that needs to be done. In other words: are you extending or replacing the overridden method? A good API documentation of the class should give you the answer. Also, libraries often follow some conventions to make it clear how to use them.

The answer to the question where to place the super call depends on when you want to execute you’re extension. Does it 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 the super method, for example modifying some object state or manipulate the arguments, you place the code before the super call. Again, the API documentation should give you the answer here.

Kdeveloper
A: 

Android will in most of the cases cause a exception if you forget to call super. I would trust that you don't have to call super if you don't do it and it doesn't throw.

In the google groups discussions some best practices for the lifecycle methods evolved(They are not officially backed by data but used by many programmers):

  • If you are in a creating method like onCreate, onResume, etc. call super as the first statement. Thus you can be sure that everything that has to be prepared from the superclass is prepared.
  • If you are in a closing method like onSaveInstanceState, onPause call super last. Now you could be sure that nothing gets removed or changed to a bad state before you got everything done.
Janusz