Reputation: 3443
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.
Upvotes: 12
Views: 8577
Reputation: 22637
as to whether to call back to the super method in the first line, or the last line of your method ... it depends on the semantic of the super method. in other words, there's no way to know other than looking at the documentation (or code) for the method. there may also be cases where you don't want to call through to the super method at all.
you seem to be asking about onResume()
. while the docs don't say, the pattern i've always used is to call back to the super method in the first line. for onPause()
, i call back in the last line. the idea is that the super methods wrap your custom pause / resume logic ...
super on resume
custom on resume
...
custom on pause
super on pause
Upvotes: 1
Reputation: 9785
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();
}
}
Upvotes: 2
Reputation: 2217
The keyword super is a way to call a parent class behavior in a specialized class.
In a constructor, calling
super(args)
let you choose which constructor of the parent class should be used.
In a method, calling
super.parentMethod(args)
let you call the parent class behavior even if current class override parentMethod. Typically it would be used to add some logic before or after the parent class.
Calling
super();
in a method won't compile since you used the constructor super syntax in a method.
Upvotes: 0
Reputation: 14238
Take a look at okPressed() method for swt dialogs. Usually people override okPressed on a dialog to do some desired work. The super.okPressed() usually disposes the dialog doing nothing. So it is called in the end after all your work is done.
public void okPressed()
{
///Do your work
super.okPressed()
}
Upvotes: 1
Reputation: 1501133
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.
Upvotes: 23