0

How can we implement two different interface which have a method with same signature.

// First Interface

interface InterfaceA{
    public int methodOne(String str);
}

// Second Interface

interface InterfaceB{
    public int methodOne(String str);
}

// Implementation class

class ImplementationClass implements InterfaceA,InterfaceB {
    
    // How can implements the both interface differently
      public int methodOne(String str){
         
      }
}

When I try to implements directly it became single implementation for the both interface's method.

How can I implement them differently ?

Jimmy
  • 995
  • 9
  • 18
  • How would you indicate which one you wanted to call? – tgdavies Oct 22 '21 at 07:42
  • Hello there, must you implement both interfaces for something bigger or something to work with some casting? You can't do what you are asking based on this example, but maybe you can do some stuff depending on the context. – alexandrum Oct 22 '21 at 07:43
  • The method name indicates behavior. Here 'methodOne' means some behavior. As both the interfaces has the same method that means both interfaces behave same way. So when a class implements both interfaces it should mean only one behavior. If you can't change method name then use some sort of composite object. – the Hutt Oct 22 '21 at 07:47

1 Answers1

2

An interface is only a "contract"

it states to a receiving method. A receiving method can define, Hey, I want to receive an object that has these methods, that accept these arguments and return this type.

If interfaces overlap, it doesn't invalidate the contract. The contract is the same, you input X, you get Y back.

public void foo(InterfaceB) {
...
}

expects an object, that implements InterfaceB, that will have, according to the contract in interface B, a method called methodOne, that returns an integer, and accepts a String as an argument.

public void bor(InterfaceA) {
...
}

expects an object, that implements InterfaceA, that will have, according to the contract in interface A, a method called methodOne, that returns an integer, and accepts a String as an argument.

Since the requirements overlap, no different behaviour is expected. You put in a String, you get an int back. How the object does this, is up to the object. The object only provides what the contract requires.

The object however, does not know via which contract it's called, and shouldn't either. If you want different behaviours for different interfaces you should use different objects for the seperate but similar interfaces.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • if he's using third party library and can't change declarations then, I think instead of inheritance a composite class could be used to get different behavior. – the Hutt Oct 22 '21 at 07:50
  • @onkarruikar yes similar case is here. We can't change the method name but want to use both. Trying with composition. if you suggest a sample snipet code would be very helpful. By the way thanx to all guys – Jimmy Oct 22 '21 at 09:41