1
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.

E.X. the right direction button will work as channel change for TV but the same button will work as skip button for VCD

Nav
  • 10,304
  • 20
  • 56
  • 83
  • Can you give an example which compiles with two implementations you want to call? – Peter Lawrey Dec 06 '11 at 17:35
  • 3
    Here is a great answer to your question: http://stackoverflow.com/questions/2801878/implemeting-2-interfaces-in-a-class-with-same-method-which-interface-method-is-o – nosirrahcd Dec 06 '11 at 17:36

1 Answers1

0

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: e.g. TVChannelChangeAction, Skip30SecAction, etc.

Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
  • what I want to achieve is explicit interface implementation..can I achieve this in java through a workaround....here is a link to what I want please check out the column name "Explicit interface implementation" in this link http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Explicit_interface_implementation – Nav Dec 06 '11 at 18:09
  • The last sentence of that paragraph says: "In Java there is no way to solve this problem other than cleaning the code and refactoring one or more of the interfaces to avoid name clashes." In these situations Java forces the developer into a design that implements a more clear [separation of concerns](http://en.wikipedia.org/wiki/Separation_of_concerns). – Dmitry B. Dec 06 '11 at 18:14
  • thats why I asked if there is a workaround...cause there is a solution for everything i believe ;) – Nav Dec 06 '11 at 18:15
  • again "there is no way to solve this problem other than cleaning the code and refactoring one or more of the interfaces to avoid name clashes" – Dmitry B. Dec 06 '11 at 18:18