Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I call super() when overriding AsyncTask's constructor?

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?

like image 437
yydl Avatar asked Jun 17 '12 21:06

yydl


1 Answers

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

like image 140
Stefan Haustein Avatar answered Sep 22 '22 09:09

Stefan Haustein