Super challenge at the end

I'm really confused about how people use super in overridden methods. Like what's different between

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

and

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

Does it do any preprocessing when we call super () at the end, because usually we call super to initialize the parent constructor.

Any performance difference with both types.

+7
source share
5 answers

First, regular super() is not allowed at all in methods - only in constructors. I assume that you actually meant super.someMethod() for the corresponding method.

Does it do any preprocessing when we call super () at the end, because usually we call super to initialize the parent constructor.

Using super in a method is slightly different than using it in a constructor.

The super (or this ) constructor requires the very first statement (if present, with implicit super() otherwise), and it can only call the constructor.

In the method, using super.someMethod() just calls the implementation of the superclass someMethod . You can call it from any method (at this time you do not need to override someMethod ), and you can call it at any time in the method. It gets called the moment you call it, it just avoids the polymorphic call of the overriding implementation.

So basically, where and how you call it depends on what you want it to be. In some cases, you may need to call an implementation of a superclass of the same method before your own custom behavior, and in some cases, you can call it after your own behavior.

+17
source

It 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 the superclass method is really important, then it will be called first. Or if the attempt to count is performed by the subclass method, but without a good result (the result is zero), an attempt is made according to the subclass method. There really is no good answer to this question, except that it really depends.


Example 1:

A label provider that extrapolates time from a Date object and returns it to the user interface for display:

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

A label provider that extrapolates a date and time from a Date object and returns it to the user interface for display:

 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 hierarchy of deep classes for user interface elements, for example

 DropDownField extends TextField extends Field extends AbstractField 

Now each of the classes added some interface 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 for writing text, etc. To get rid of the elements, you will have to perform a multi-level deletion as follows:

 public class DropDownField extends TextField { @Override public void dispose() { menu.dispose(); arrow.dispose(); //let text field dispose of its elements super.dispose(); } } 
+2
source

Take a look at the okPressed () method for swt dialogs. Usually people override okPressed in the dialog box to do some kind of work. In super.okPressed (), a dialog box usually does nothing. Thus, it is called at the end after completing all your work.

 public void okPressed() { ///Do your work super.okPressed() } 
+1
source

The super keyword is a way to invoke the behavior of a parent class in a specialized class.

In the constructor calling

 super(args) 

allows you to choose which constructor of the parent class to use.

In the method calling

 super.parentMethod(args) 

allows you to invoke parent class behavior, even if the current class overrides parentMethod. It will usually be used to add some logic before or after the parent class.

Call

 super(); 

in the method will not compile since you used the super constructor syntax in the method.

0
source

as to whether to switch to the super method in the first line or the last line of your method ... it depends on the semantics of the super method. in other words, there is no way to know but to look at the documentation (or code) for this method. There may also be times when you don’t want to go over to the super method at all.

you seem to be asking about onResume() . while the docs don’t say, the template that I have always used is to translate the super method to the first line. for onPause() , I'll call you back on the last line. the idea is that super methods wrap your user pause / resume logic ...

 super on resume custom on resume ... custom on pause super on pause 
0
source

All Articles