0
votes

I got a question about an abstract class.

I'm developing an android application and wrote an abstract class which extends the AsyncTask<Void, Fragment, Fragment> class. The doInBackground method has already a body in my abstract class but I don't want to return anything here. Now my subclass that extends that abstract class should override that doInBackground method again, call super, do some work and then return a value.

the method in my abstract class looks like this:

@Override
protected Fragment doInBackground(Void... params) {
    //do some work without returning a value
}

and the method in my extending subclass looks like this:

@Override
protected Fragment doInBackground(Void.. params) {
    super.doInBackground();
    MyFragment fragment = new MyFragment();
    //do some work with the fragment
    return fragment;
}

So if I do not have a return statement in my abstract class Android Studio flags my method with an "Missing return statement" error. Is there a way to avoid this error message, because my subclass has a return value and so it should be no problem, should it?

2
I don't think you should be returning a Fragment. That is a UI element. Instead return some data to be displaying within the UIOneCricketeer
I agree with this comment.apelsoczi
In my app the fragment is prepared by the AsyncTask. Then in onPostExecute I call a method in my activity to show the fragment. The AsyncTask is started by my activity so I think it should not be a problem.Michael S.

2 Answers

1
votes

Note: This is a general answer, ignoring the specific case shown in the question, which may or may not make sense (see comment).

If the method is supposed to return a value, but the implementation in the superclass can't return a value, then it is not an implementation of that method.

If you still want some common code in the superclass, write that code in a separate method.

abstract class Super {
     public abstract MyObj method();
     protected final void commonLogic() {
         // common logic here
     }
}
class Sub extends Super {
    @Override
    public MyObj method() {
        commonLogic();
        // code here
        return /*something*/;
    }
}

If you want to enforce that the common logic is execute, use the template method pattern.

abstract class Super {
     public final MyObj method() {
         // common logic here
         return afterCommonLogic();
     }
     protected abstract MyObj afterCommonLogic();
}
class Sub extends Super {
    @Override
    protected MyObj afterCommonLogic() {
        // code here
        return /*something*/;
    }
}
0
votes

In reference to the Ajay P. Prajapati answer - I don't think this solves his requirement correctly. AsyncTask uses the java object Void in a round about way to assimilate to void in the Java language - notice the difference of an upper case and lower case V. My recommendation is to change to AsyncTask<Void, Object, Object>, in your abstract class you can return new Void() and in your subclass you can return fragment.