简体   繁体   中英

Overriding in Java/Android (class -> anonymous class) doesn't work.. why?

I've a simple inheritance problem.. but I can't solve it.

This is the basic class:

public abstract class RpcOkCallback extends RpcTupleCallback
{
    // [...] constructor [...]

    public boolean callback(int responseCode, final String module, boolean flag){
        if (flag){
            return onResponse(responseCode, module);
        } else {
            return onError(responseCode, module);
        }
        return false;
    }

    protected abstract boolean onResponse(int responseCode, String module);

    protected boolean onError(int responseCode, String module){
        return true;
    }
}

And this is an anonymous class that redefine the base class:

new RpcOkCallback("color_seek_ir", "set_flash_ir"){
        @Override
        protected boolean onResponse(int responseCode, String module) {
            if (seekBar != null) seekBar.setEnabled(true);
            return true;
        }

        @Override
        protected boolean onError(int responseCode, String module) {
            if (seekBar != null) seekBar.setEnabled(true);
            return true;
        }

    }

The question is.. why when the onResponse method is called it calls correctly the overridden method while when it calls the onError method, it calls the base case (the "return true" method)? I've tried to declare abstract the "return error" method too and it works... but I don't want to declare in every anonym class a basic method like that.

Any idea? Thanks :)

I found the problem. It actually works correctly (the overriding part), but the callback was already handled by another "onError" method (not overridden) and because of the "return true" the event was canceled before reaching the final destination :(

Adding the abstract and defining all the methods did the job, so thanks corsair (if you want to add your answer or what I will mark as solved the question by you :) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM