I have this code:
public interface AsyncResponse {
abstract void processFinish(JSONObject output);
abstract void processFinish(String output);
}
I use it like this:
new AsyncResponse(){
@Override
public void processFinish(JSONObject output) {
//code
}
@Override
public void processFinish(String output) {
//code
}
}
When I overload only one method(for example only the JSONObject) it gives the error:
"Class 'Anonymous class derived from AsyncResponse' must either be declared abstract or implement abstract method 'processFinish(String)' in 'AsyncResponse'"
If it is possible, I need to overload only one method, not both.
I don't know if it's something similiar in AsyncTask where only the doInBackground is required to be Override-ed and the onther methods are optional.
Not sure if I have used the right programming terms for my question, but I hope that, what I need to do is clear.
It is not possible unless you split the interface into two:
public interface AsyncResponseWithJSONObject {
void processFinish(JSONObject output);
}
public interface AsyncResponseWithString {
void processFinish(String output);
}
The classes that can handle strings implement the latter and classes that can handle JSON implement the former.
Then why can
AsyncTaskdo it?
AsyncTask is an abstract class, not an interface. When extending an abstract class, you only need to implement abstract methods. Interfaces on the other hand requires all methods to be implemented. (because all methods in an interface are implicitly abstract)
So you can try using an abstract class as well:
public abstract class AsyncResponse {
public void processFinish(JSONObject output) {
}
public void processFinish(String output) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With