Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing multiple interfaces having same method

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();
like image 590
Sid Avatar asked Jul 05 '13 08:07

Sid


People also ask

How could a class implement two interfaces that has the same method?

Interfaces only proscribe a method name and signature. If both interfaces have a method of exactly the same name and signature, the implementing class can implement both interface methods with a single concrete method.

Can a method implement multiple interfaces?

Yes, a class can implement multiple interfaces. Each interface provides contract for some sort of behavior.

Can two interfaces have same default method?

Multiple Defaults With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.

Can we implement 2 interfaces in Java?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).


3 Answers

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

like image 166
JREN Avatar answered Oct 16 '22 22:10

JREN


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.

like image 31
user4574 Avatar answered Oct 16 '22 22:10

user4574


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.

like image 6
andrewdotn Avatar answered Oct 16 '22 20:10

andrewdotn