• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Overriding

vag

Newbie
Hi all.
When we override method in subclass, we call in this method the superclass method, for example
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w ;
height = h ;
Log.d(TAG, "onSizeChanged: width " + width + ", height "+ height);
super.onSizeChanged(w, h, oldw, oldh);
}
so why we need to call super.onSizeChanged().
I delete the line super.onSizeChanged(), and the result is the same as with it.


or the same in onCreate method, we call super.onCreate.

sorry for bad english, and thaks for replays))
 
I *think* it goes something like this (please correct me if wrong)

In Android, you MUST call through to the super class for component lifecycle methods that you override.

Such as (as QuackWave said) onCreate(), onDestroy(); onResume(); onStart(); onStop(); etc

The reason you must do this is because Android has to do work behind the scenes (such as loading your app into memory). So it must do its work before you can do yours.

Almost everything else would be optional to call through to the super method.

In some cases the super method will do nothing. In others, the super method will help process something.

a good example would be onTouchEvent()

You could process the event if it's a specific type of event and NOT call super.

Or, if it's not the event you want, you could hand it off to the super class to process normally.
 
my two cents....

"super" must be (not compile otherwise) at first line in method only for constructor ... other overridden method can place "super" anywhere in the method ... for sure for the framework(android here) that may mean something important .. but may be not forbidden if you know what you do ;)

and sometime in not so rare case you may want to not invoke super ... to be able to replace the complete method behaviour ... I think this is not the case in current android framework .. but on other home made classes it may appends.


hope to give more light ...

hope that help

have fun
have fun
 
Back
Top Bottom