Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super at end

i am really confused by the way people use super in overridden methods . Like what is the different between

  @Override
  protected void onResume() {
    // some logic here 
    super.onResume();
  }

and

  @Override
  protected void onResume() {
    super.onResume();
    // some logic here 
  }

Does it perform any pre-processing when we call super() at the end , because generally we call super to initialize parent constructor.

Any difference in performance with both kinds.

like image 818
kaushal trivedi Avatar asked Jun 26 '13 20:06

kaushal trivedi


People also ask

Do you need to call super ()?

However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.

What does super (); do?

The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.

Does super () init have to be first?

If the class you're super ing requires the object to be in a certain state then you put it in that state before super ... If your class requires the object to be in a certain state before it can continue its initialisation and that's provided by a parent class then you super that first.

What is super () in constructor?

The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.


2 Answers

Firstly, plain super() isn't allowed in methods at all - only in constructors. I assume you actually meant super.someMethod() for an appropriate method.

Does it perform any pre-processing when we call super() at the end , because generally we call super to initialize parent constructor .

Using super in a method is somewhat different to using it in a constructor.

In a constructor, super (or this) is required to be the very first statement (if present, with an implicit super() otherwise) and it can only call a constructor.

In a method, using super.someMethod() simply calls the superclass implementation of someMethod. You can call it from any method (you don't have to be overriding someMethod at the time) and you can call it at any point in the method. It's invoked at the point you call it, it just avoids the polymorphic call to an overriding implementation.

So basically where - and whether - you call it depends on what you want the effect to be. In some cases you may want to call the superclass implementation of the same method before your own custom behavior, and in some cases you may want to call it after your own custom behaviour.

like image 129
Jon Skeet Avatar answered Sep 20 '22 11:09

Jon Skeet


That really depends on the logic that the method implements. Sometimes it is called first, sometimes at the end, and sometimes never. For example, if some calculation from superclass method is really important, then it will be called first. Or if calculation is attempted in subclass method but without a good result (result is null) attempt is made on subclass method. There is really no good answer to this question except that it really depends.


Example 1:

A label provider that extrapolates time from the Date object and returns it to the UI for showing:

public class TimeLabelProvider {
    public String getText(Date element) {
        //extrapolate time from the element
        return time;
    }
}

A label provider that extrapolates both date and time from the Date object and returns it to the UI for showing:

public class DateTimeLabelProvider extends TimeLabelProvider {
    @Override 
    public String getText(Date element) {
         //extrapolate date from the element
         String date = ...

         //now get time string. We don't need to repeat logic, 
         //its already implemented in super method!
         String time = super.getText(element);
         return date + " " + time;
    }
}

Example 2:

If your project has a deep class hierarchy for UI elements, e.g.

DropDownField extends TextField extends Field extends AbstractField

Now, each of the classes added some UI elements to the field, for example, DropDownField added a menu and a little arrow to the right of the field, TextField added a validator, AbstractTextField added a white block to write text, etc... To dispose elements you would have to do multilevel disposal like this:

public class DropDownField extends TextField {

    @Override
    public void dispose() {
        menu.dispose();
        arrow.dispose();
        //let text field dispose of its elements
        super.dispose();
    }
}
like image 43
darijan Avatar answered Sep 19 '22 11:09

darijan