'What's the difference between calling super.onCreate() and calling through to the superclass? [closed]
The following screenshot is from official Android documentation of android.app.Application#onCreate() which says If you override this method, be sure to call super.onCreate() and then If you override this method you must call through to the superclass implementation. (I marked them with red arrows on image). What is the difference between these two statements?
Solution 1:[1]
Calling super.onCreate()
is calling through the superclass. You'll see that the Activity class has onCreate annotated with @CallSuper
which forces this call.
Solution 2:[2]
They are equivalent.
The reason that the documentation for Application.onCreate
has both lines is that originally, it was a line in the Javadoc documentation. This is responsible for the original "If you override this method, be sure to call super.onCreate()".
The second one is added by the presence of the @CallSuper
annotation, which was added later. Note that "If you override this method you must call through to the superclass implementation." does not appear in the source code linked above.
At this point, the line in the documentation comment could be removed, as it's now redundant.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ryan M |