Implementing multiple interfaces having same method

22,947

Solution 1

Because it's an interface there is no harm done. You're basically using a blueprint for your C class by implementing A and B. Both A and B say that C should implement a method called test()

Your C class implements that method, so the interfaces have done their job.

It's basically your C class saying: "Oh hey, I need to implement test() because of interface A" and you implement it. Then your C class says "Oh hey, I need to implement test() again because of interface B" and it sees that there is already a method called test() implemented so it's satisfied.

You can also find more information here: JLS §8.4.8.4

Solution 2

Suppose we have two interfaces...

public interface StockBroker{
        //Give our client some investment strategies.
        public String adviseClient(Client c);
}

public interface Doctor{
  //Examine our client and give them some medical advice
        public String adviseClient(Client c);
}

And a class implementing both interfaces....

public class JackOfAllTrades implements StockBroker, Doctor{
   public String adviseClient(Client c){
   }
}

While it may be syntactically correct to implement both interfaces with one method, you may not get the desired behavior. For example, a stock broker and a doctor typically each give their clients vastly different advice.

Someone using an object that implements the interface Doctor expects the adviseClient() method to give medical advice. But someone using an object that implements the interface StockBroker expects the adviseClient() method to give out investment strategies.

In this case, the object JackOfAllTrades does not know what type of advice to give out because the adviseClient() method has no parameters telling it which interface it is supposed to be implementing when adviseClient() is called.

This is a shortcoming in Java because the person designing the Doctor interface may have had no way of knowing that someone else would design a StockBroker interface with the same method signature.

To anyone creating interfaces, its probably good practice to make the method names unique enough that name collisions are rare.

Solution 3

JLS §8.4.8.4 says,

Inheriting Methods with Override-Equivalent Signatures

It is possible for a class to inherit multiple methods with override-equivalent signatures (§8.4.2)

There might be several paths by which the same method declaration might be inherited from an interface. This fact causes no difficulty and never, of itself, results in a compile-time error.

It seems the rationale was that if a class has multiple declarations with the same name and signature, since the class may have inherited them through multiple paths—implementing an interface and also subclassing a class that implements that interface, for example—no harm is done.

Solution 4

Not as far as syntax is concerned but if the intent of one of the methods is not adhered to, its contract is broken and the code can be considered as broken.

Using your analogy, if I promised Michael to wear a blue shirt instead of a red shirt, and I can't wear two shirts, then I will have to break at least one promise.

The same can hold for the methods: if keeping one contract would mean breaking the other then it's in fact a bad idea to implement both interfaces.

Edit:Contract broken, As per Class C signature It should implement two methods,but ultimately its implementing only one method and omitting another.

Reference

Solution 5

interface A
{
void test();
}

interface B 
{
void test();
}
class C implements A, B {

    public void test() 
    {
        System.out.println("common to all");
    }
    public A choose(A a){
        return new A(){
           public void test() {
                System.out.println("test of A");
           }
        };
    }
    public B choose(B b){
        return new B(){
           public void test() {
            System.out.println("test of B");
           }
        };
    }
}
class Demo {
   public static void main(String[] args) {
   C c =new C();

   A a = new C();
   B b = new B();

   a = c.choose(a);
   b = c.choose(b);

   a.test();
   b.test();
   }
}
Share:
22,947
Sid
Author by

Sid

Passionate about technology and startups. Love to work with smart people.

Updated on December 23, 2020

Comments

  • Sid
    Sid over 3 years

    This code works perfectly. The method test() works for both interfaces. What is exactly going on under the hood? And how is this feature useful in practical scenario?

    interface A
    {
        void test();
    }
    
    interface B 
    {
        void test();
    }
    
    class C implements A, B
    {
    
        public void test() 
        {
            System.out.println("abc");
        }
    }
    
       A a = new C();
       a.test();
       B b = new C();
       b.test();