Hi all, I have a simple question. I have declared two methods in two different interfaces. The return of is same for both the methods i.e they are same. I implement both the interfaces in a class. And I also use the methods in the class. Now Will it compile .... -> If it does then how will the class know which interface has to be looked for the method. Any clues shall be put at the top of Appreciation .... Bye !!!
Hi Soman You would be required to spell out the exact path. That is, if you have callStack() method in both ProdArray interface and AssyArray interface then you would be required to call it like ProdArray.callStack() and AssyArray.callStack().
Anumpam -- No, totally wrong. Java isn't C++. Soman -- This case is explicitly addressed in the JLS. The answer is that if the methods can both be satisfied by a single implementation (i.e., their exception signatures are identical too, or close enough) then this will compile and work; but if the return types or exception signatures are incompatible, then it simply can't be done. The question of "how does it know which to call" can't and won't come up. Java only has single inheritance, so there can only ever be at most a single inherited version of a method, which you can override, or not. There's no way for code outside of a class to call an inherited method that's been overridden. So whichever interface you think you're using, there will only be a single method, and it better make sense for both interfaces!
Originally posted by Anupam Sinha: You would be required to spell out the exact path. That is, if you have callStack() method in both ProdArray interface and AssyArray interface then you would be required to call it like ProdArray.callStack() and AssyArray.callStack().
Actually no. If you implement two interfaces with the same method then no one cares that you are only implementing one method. The interface contract has been satisfied for both. The only problem is if the methods have different return types. In that case you will not be able to compile your code.