By rgr_mt

2011-02-14 17:05:43 8 Comments

In many of Android methods, especially constructors and overridden methods, you should or even must call the parent class method using super(). When you use the Eclipse Source > Override/Implement Methods... you get code from a template with TODO tags like this:

public MyCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub      

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags. In the example, I would call super() before my code in the constructor and after my code in onDraw().

Can I always rely on these code insertions locations in the generated code? Is there a simple rule/explanation when to call super()?


@Ted Hopp 2011-02-14 17:21:56

This is a good question. Unfortunately, there is no simple rule for this. You need to know what the superclass implementation does. Sometimes (as in View.onDraw()), the superclass implementation does nothing; calling super() is both harmless and unnecessary. In other cases (such as Activity.onCreate()) the superclass implementation performs critical operations that must be executed at some point in the subclass's processing. Sometimes what happens when you call super() should come before any processing in the subclass, sometimes at other points. Sometimes you want to completely replace the superclass processing with your own, in which case you don't call super() at all. You have complete freedom to call the superclass version at any point (or even at multiple points) in your subclass's logic.

In constructors, the call to a superclass constructor (if present) must be the first thing in the method. If you don't have one, the compiler automatically inserts a call to the no-argument constructor in the superclass. (If the superclass does not have a no-argument constructor, or if it is not accessible to the subclass, the compiler generates an error.)

If the documentation doesn't provide enough information, then you have to look at the source code. The Android code is available here (Gingerbread release). The API code is under core.

EDIT The code is no longer available at Here are two other places where you can browse the code:

The main code is in the repository Platform > Frameworks > Base

@Cristian 2011-02-14 17:14:10

Can I always rely on these code insertions locations in the generated code?

No, sometimes you don't want to call the super.method. Sometimes you want to call it first, sometimes at the last place, etc. It depends. But, I'm talking about methods, no constructors.

Is there a simple rule/explanation when to call super()?

You will always have to all super as the previous answer points. The only case where you don't call super is when the constructor of the super class has no parameters; in that case the compiler will put the super for you.

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags

If you are in doubt (I'm talking about super methods), you can always take a look at the source code. Google Code Search is a good resource to do so. Then you can decide whether to put your code before or after the super method; or even, don't put the super method at all.

Keep in mind that not putting the super method is valid at compile time. But, some methods on android won't work unless you invoke the super method (for instance, the onResume method of the Activity class).

Also, sometimes you will decide whether to run the super method or not in runtime. Consider this classic example:

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode() ){
        return true;
    return super.onKeyUp(keyCode, event);

If the user pressed the back key, you won't call the super method. If the user didn't, you delegate the work to the super method.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Eclipse/Java code completion not working

  • 2009-05-26 00:43:45
  • Robert Brown
  • 367863 View
  • 594 Score
  • 23 Answer
  • Tags:   java eclipse ide

44 Answered Questions

[SOLVED] Can't start Eclipse - Java was started but returned exit code=13

14 Answered Questions

[SOLVED] Is there a way to collapse all code blocks in Eclipse?

  • 2009-11-13 01:12:18
  • ambertch
  • 215462 View
  • 539 Score
  • 14 Answer
  • Tags:   eclipse ide

12 Answered Questions

27 Answered Questions

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

3 Answered Questions

[SOLVED] What causes OnDraw method of View to be called and when

43 Answered Questions

[SOLVED] Update Eclipse with Android development tools v. 23

Sponsored Content