简体   繁体   中英

can I call an interface method explicitly in java? If not then is there a workaround for it?

public class a implements b,c {

    public void g()
    {

    }

    public static void main(String[] args){

              a object =new a(); //this overrides for both the methods in            
                                // the interfaces b and c
          object.g();

    }

}
interface b 
{
    void g();
}
interface c
{
    void g();
}

Here I want to provide different implementations for the two interfaces . How do I do it. In c# you can explicitly specify the overridden method is meant for which interface.Can i do that in java.If not please specify a workaround this problem with a simple example.

What I want can be stated like this... supposing the interfaces are meant for one TV remote(interface b) and a internal VCD player remote (interface c)...and when I am implementing the features of both in one remote (class a in this case)I want to provide different implementation for the TV and the VCD for the same button clicked on the remote(method g() in this case). When I override g() it gets overridden for both the interfaces.How do I explicitly mention which interface it is meant for. I want to provide different implementation(by overriding g()) for TV and VCD player.

EX the right direction button will work as channel change for TV but the same button will work as skip button for VCD

This is the answer to the edited question:

  1. An interface simply says "this class implements method X." It doesn't matter how many interfaces that declare the same method you put on a class the result is still the same: "this class implements method X."

  2. In your case, the problem is with the conceptual design. The behavior of the button is actually the same whether it's on a TV or VCD: it fires an action when pressed. That should be the behavior of your method: find an action associated with the pressed button and execute it. See how in this case it will be the same for TV and VCD? Now, the class encapsulating the the action behavior is different. You may have a number of this classes: eg TVChannelChangeAction, Skip30SecAction, etc.

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