Why do we have to call super in Android sometimes?

46,961

Solution 1

Why are we forced to call super.method()?

The classes that make up the Android SDK can be incredibly complex. For instance, both activities and fragments must perform a number of operations in order to function properly (i.e. managing life cycle, optimizing memory usage, drawing the layout to the screen, etc.). Requiring the client to make a call to the base class (often at the beginning of the method) ensures that these operations are still performed, while still providing a reasonable level of abstraction for the developer.

How do we know that a function method requires super to be called?

The documentation should tell you whether or not this is required. If it doesn't I'd Google-search for some sample code (or check the API demos... or better yet, look at the source code!). It shouldn't be too difficult to figure out.

Solution 2

I know this is not the true intention of the OP, he did ask this question and I don't believe it got a very good answer so, if anybody is ever wondering "Why the heck do I HAVE to call super?!? If they're going to require it, why don't they just do it for me!!!". Here's the answer to that questions....

Basically, super() is something that must be called if you're overriding something that MUST be called, which can often be rather complicated code. Now, the reason they don't just do it for you and call it before your function is mostly so that you have control! :-D

To be more specific, you cannot control IF you call super(), however, you can control WHEN! So, let's say you have some specific code that needs to happen BEFORE super() gets called, you now have the freedom to call super() only after running your code. Or... let's say you require super() to have already ran for your code not to crash, you now have the option to run super() before running your code, hence ensuring that everything is set up for you. Heck, you could even technically override the superclass hardcore and code your own method that takes care of super() itself, and make it so you don't have to call super(), but 99.9% of the time sane people will not need to do this! :-P

So, the short answer to "why do I have to call super()" is... So that you can control when it's called and do things before, or after super() gets ran. :-)

Solution 3

The super keyword has two main uses

1. Calls the superclass’ constructor.

2. Access a member of the superclass that has been hidden by a member of a subclass.

So, why need to user super keyword sometimes ? Answer would be android comes under 4GL language which means it has many functionality ready made. While we are overridding these methods for the customization we use super keyword.

see the very simple usage of super keyword in android ( as we do it most of the time ).

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    .
    .
    .
}

super() must always be the first statement executed inside a subclass’ constructor. When a subclass calls super(), it is calling the constructor of its immediate superclass. The second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.

Solution 4

The requirement is generally specified directly in the API documentation. For example, see android.widget.ListView.onFinishInflate:

protected void onFinishInflate ()

...

Even if the subclass overrides onFinishInflate, they should always be sure to call the super method, so that we get called.

Unfortunately, my personal experience is that Android docs are uneven in quality. So, I suspect there are cases where the call is required but not documented as such.

Solution 5

It is important to note that once you override a method, you basically ignore everything that was in the parent class and instead have your own custom implementation in the child class (literally overwriting it)!

In our case, we don't want to throw away the parent implementation. We actually want to continue to use the original method, and ADD the extra checks for each child class individually.

This is where we get to use the "super" keyword!

You are allowed to re-use the parent method in the child class by using the "super" keyword, followed by a dot and then the method name:

for example: isValidMove(position) is method for chess pieces & check move validity & bound in the 8x8 chess board.

super.isValidMove(position);

Using the keyword super here means that we want to run the actual method in the super (or parent) class from inside the implementation in "this" class.

Which means in each of the child classes, before you get to check the custom movement, you can check if super.isValidMove(position) has returned false. If so, then no need to do any more checks and immediately return false; otherwise, continue checking.

The new implementation for the Rook class will look like this:

class Rook extends Piece{
   boolean isValidMove(Position newPosition){
      // First call the parent's method to check for the board bounds
  if(!super.isValidMove(position)){
     return false;
  }
  // If we passed the first test then check for the specific rock movement
  if(newPosition.column == this.column && newPosition.row == this.row){
     return true;
  }
  else{
     return false;
      }
   }
}

You can also use super() to call the parent's constructor. This is usually done when implementing the child's constructor. Typically you would want to first run everything in the parent's constructor then add more code in the child's constructor:

class Rook extends Piece{
   // default constructor
   public Rook(){
    super(); // this will call the parent's constructor
    this.name = "rook";
   }
}

Note: If a child's constructor does not explicitly call the parent's constructor using super, the Java compiler automatically inserts a call to the default constructor of the parent class. If the parent class does not have a default constructor, you will get a compile-time error.

Share:
46,961

Related videos on Youtube

StackOverflowed
Author by

StackOverflowed

I use stack overflow when i can't figure out the answer.

Updated on July 09, 2022

Comments

  • StackOverflowed
    StackOverflowed almost 2 years

    Sometimes when I override methods, I get an exception the first time it's called like below:

    05-31 21:32:04.266: E/AndroidRuntime(28471): android.support.v4.app.SuperNotCalledException: 
    Fragment AnalFragment{41795860 #1 id=0x7f070002} did not call through to super.onDestroy()
    

    Why are we forced to call super.method()? It makes sense that there are obligations by the parent class, but more importantly, how do we know that a method requires super to be called, rather than waiting for it to crash?

    • Natix
      Natix almost 12 years
    • Alex Lockwood
      Alex Lockwood almost 12 years
      In Java, we have methods, not functions. Remember that a method is a function that belongs to a class (a member function).
    • Dave Newton
      Dave Newton almost 12 years
      @AlexLockwood So... methods are functions, and we don't have functions?
    • Alex Lockwood
      Alex Lockwood almost 12 years
      Methods are "functions that belong to a class".
    • Alex Lockwood
      Alex Lockwood almost 12 years
      I'm not sure exactly how to define it without using the word "function"... how about "subroutine" instead? All I know is Java does not have functions, haha
    • Alex Lockwood
      Alex Lockwood almost 12 years
    • StackOverflowed
      StackOverflowed almost 12 years
      Sorry you're right, switching between PHP and Java crosses up my lingo sometimes.
    • ajacian81
      ajacian81 almost 12 years
      +1 for "AnalFragment" - I'm curious as to what you're writing.
    • donfede
      donfede over 11 years
      The methods/functions discourse is rather myopic (and frankly irrelevant to the target discussion at hand). Both terms are generally valid in this context.
  • StackOverflowed
    StackOverflowed almost 12 years
    I guess my relying on Eclipse's autocomplete will always expose me to this issue. I guess my question is, how do you force a child to call super?
  • Alex Lockwood
    Alex Lockwood almost 12 years
    By "requiring", I meant that if you don't call super, either the runtime system will throw an exception or things just won't work properly.
  • StackOverflowed
    StackOverflowed almost 12 years
    No sorry, I mean what is the method to force a requirement of calling super?
  • Alex Lockwood
    Alex Lockwood almost 12 years
    This depends on the base class implementation. Most of the time, Android checks to see that the superclass method was called with a boolean flag. For instance, in the Activity implementation, there is a boolean variable mCalled that is set to true if the superclass method has been called, and false otherwise. If the base class recognizes that the superclass method was never called, it throws a SuperNotCalledException. You can see for yourself by checking the Activity source code. Does that answer your question?
  • StackOverflowed
    StackOverflowed almost 12 years
    Ahh I see, it's a secondary method that enforces this behaviour. Thanks.
  • Mike Lowery
    Mike Lowery over 7 years
    Note: If a constructor does not explicitly invoke a superclass constructor, the Java (and also C#) compiler automatically inserts a call to the no-argument constructor of the superclass. This is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
  • AlxDroidDev
    AlxDroidDev over 7 years
    @StackOverflowed I know this is old, but you can force a method to call super with the annotation @CallSuper in the parent's implementation. This way, Lint will generate an error if the extending class' implementating overrind that method doesn't call super.
  • TylerH
    TylerH about 6 years
    What does this answer add to the existing answers?
  • Rahul Joshi
    Rahul Joshi about 6 years
    TylerH - Simplicity to understand. All above answers are like jargon to any new bee developer. This question always arises to the new developers. Here I tried to connect them with what the basics they have learned OOP so as to get some acquaintance. Above answers are amazing & cool but for they are easy for the understanding of medium grade dev. not a new bee dev as he/she may ask what the heck is this super? It is a like superman. I hope you understood my point.