11

In many examples I have seen online, AsyncTask is extended, the constructor is overriden, and super() isn't called. For example, in this answer by hackbod:

static class Worker extends AsyncTask<URL, Integer, Long> {
    MyActivity mActivity;

    Worker(MyActivity activity) {
        mActivity = activity;
    }

    [...]

}

the new constructor does not call back to the parent's constructor.

There's similar code in this sample project by CommonsWare.

So is this correct? Or should super() really be called?

Community
  • 1
  • 1
yydl
  • 24,284
  • 16
  • 65
  • 104
  • 1
    Take a look at the constructor of ASyncTask and see if this is something you want to use: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/AsyncTask.java – Blundell Jun 17 '12 at 21:41

1 Answers1

18

The default super constructor is called implicitly if super() is not called explicitly. So there is really no difference at all. I'd use the shorter version (omit super()), which seems to be common practice in Java.

p.s. See also this thread: Default constructors and inheritance in Java

Community
  • 1
  • 1
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
  • so ok, i need to improve my english to use more specific words... >/ – Simon Dorociak Jun 17 '12 at 21:49
  • 1
    @Stefan, FYI many methods in the Android framework will throw an error if you have not called super explicitly, such as in `onCreate` (providing rationale for the question) – Kristopher Micinski Jun 17 '12 at 21:55
  • @KristopherMicinski super() or a super constructor that takes additional parameters? There should be only a difference if there is an alternative super constructor with parameters. – Stefan Haustein Jun 17 '12 at 21:57
  • I am not speaking wrt constructors, but methods. Anyway, I'm not contesting your answer, just trying to provide rationale for why it was asked. – Kristopher Micinski Jun 17 '12 at 21:58