0

Old Question:

I've read somewhere that implementing two interfaces with similar method signatures in same class, isn't a good choice and can lead to problems. A trivial program like underwritten works fine. Then, what kind of problems? Is it Undefined Behaviour or is it that it leads to design issues? I couldn't get it. Something from the language specifications on this will be helpful.

/* ------------------ */
public interface IA {

    public void method();
}
public interface IB {

    public void method();
}
/* ------------------ */
public class C implements IA, IB
{

    @Override
    public void method() 
    {
        System.out.println("class implementing two interfaces");
    }
    public static void main(String args[])
    {
        new C().method();
    }
}

Edit:

I'm aware of the diamond problem and if I'm not wrong then Java avoids it. My point was different. So as an edit, I'm rephrasing my question as, since, "implementing two interfaces with similar method signatures" in same class is perfectly valid in java, then why is it considered to be a bad choice to do so? from the perspective of design, and any other issues that might prevail in such a practice.

WhiteSword
  • 101
  • 9
  • Duplicate question: https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method – Bryan Aug 18 '17 at 14:54
  • 1
    Possible duplicate of [Implementing two interfaces in a class with same method. Which interface method is overridden?](https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method) – Debanik Dawn Aug 18 '17 at 14:58
  • @DebanikDawn . thanks for the link, but the questions are somewhat different on the grounds that my question asks for reasons why doing it is a bad practice & where could the problems occur. – WhiteSword Aug 18 '17 at 15:11
  • why a downvote, any particular reasons? :\ – WhiteSword Aug 18 '17 at 15:12
  • @Marv Since, I'm from C background, there are so many things which are left unclear in the C standards and leads to UB. So, I thought it could be a case here as well, only if the language documentation doesn't give a proper description if such an issue occurs. Please, make me correct if I'm wrong here. And i.e. why I asked for a reference from language specs. – WhiteSword Aug 19 '17 at 02:40

3 Answers3

1

I think the main issue here is a design issue. The same method name probably has different semantics for different interfaces. For example, if you implement the interfaces Runnable and Player and both have a run method, what should the implementation look like? Runnable#run is a generic method to run a snippet of code, while Player#run is probably meant to increase the players movement speed.

Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
0

This is a classic programming problem, which was first introduced to me in C++. It is known as the "diamond pattern inheritence"

Java avoids the diamond pattern inheritence problem by having Interfaces only demand that the method be present, but the name of the interface isn't the dermining factor for the implementation's location. Instead the bottom most implementation in the Type tree is always used.

So, You can have a dozen Interfaces all declaring run() but regardless of which one you use, they all will call the base-class definition of run().

Now, with updates to Java's language for "default methods" in an interface, it gets a bit more complicated, but the core reasons why this is permitted date back to before "default methods" on an interface were permitted.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

The same signature technically should be ok, as in your example. But similar signature, i.e. different return type may cause compilation error, like described below: Implementing two interfaces in a class with same method. Which interface method is overridden?