3

I have tried to google this one, but I can't find an acceptable answer. Is interface inconsistency, when you make a class implement 2 or more interfaces which are incompatable with each other? For ex:

public interface Lion()
{
    public void eat();
}

public interface Tiger()
{
    public void eat();
}

public class Liger implements Lion, Tiger
{
    public void eat(); //Problem: How does it eat? Like a lion or tiger?
}

Am I correct or way off base?

LTH
  • 1,779
  • 3
  • 19
  • 21
  • Unfortunately there is no direct way to resolve this issue. Related threads : http://stackoverflow.com/questions/6211070/class-inheriting-from-several-interfaces-having-same-method-signature and http://stackoverflow.com/questions/2598009/method-name-collision-in-interface-implementation-java – KV Prajapati Dec 16 '11 at 02:56
  • 1
    this problem in C# is solved by explicit implementation of interfaces.. – Srinivas Reddy Thatiparthy Dec 16 '11 at 02:57

2 Answers2

3

In Java, you are guaranteed that an two interface methods which compile to the same function return the same "type".... Thus, in this context, interface inconsistency can refer to :

  1. When you implement 2 methods that implement the exact same function, with different side-effects , or diferent underlying assumptions/algorithms that are not expressible in the method signature... i.e. two methods that "look" the same but that "do" different conceptual tasks.

  2. There is also the (non-java specific) GUI connotation, wherein the user experience is confusing, with similar components being utilized for different tasks (or vice-verse, the same task being triggered by different GUI components).

The solution to 1 is to have a more expressive interface, or more precise function names (to exemplify a more sophisticated interface: maybe the Lion and Tiger should provide an Eater object, which is capable of eating in one or more different ways).

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
jayunit100
  • 17,388
  • 22
  • 92
  • 167
0

I think the following thing makes more sense,

public interface Animal{
     public void eat();
}

public class Lion implements Animal{
     //code
}

public class Tiger implements Animal{
     //code
}

public class Liger extends Lion /*or Tiger*/{
     public void eat(){}
}

or not ?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
gprathour
  • 14,813
  • 5
  • 66
  • 90