Your remark about contracts is correct, you have only one contract, but then it is not correct that there will be invocation of the method of one of the interfaces, there is no method in them, only contracts of methods (even has this from Java 8, but it is not a case that the question wants to know). The invocation will be the method that is in the class, that’s all.
The invocation can occur on any object that is of the class type, of its descending types (although it can be a descending method and not exactly of this class) or it can occur when using one of the two interfaces, which will call the class method with the same signing.
As Java has no explicit implementation of interface method it has no way to differentiate one or the other, so invoke by InterfaceA
or by InterfaceB
will call the same method. It has language, like C# for example, that allows each one to have its own implementation after the most likely is that you have very different goals between them although the method coincidentally has the same signature. Almost always if you have two interfaces with a method with the same signature and they have to do the same thing is error of design and Java cannot adequately handle the right cases where it has the same signature but different objectives.
So whether it makes a difference or not depends on what you’re doing. If the design is bad makes no difference, if in fact each method has different goal you will have to deal with it within the implementation of the method making the code less robust and slower.
Why would you make two interfaces with the same signature and goal? Or it is a hypothetical case that does not occur or is something poorly thought out (some cases that escape its control). Even the cases that the objectives are different you can think if there was a good choice there, it may make sense in some cases, but if it happens a lot it is also something badly architected.
Opa, it was just a hypothetical case. A doubt that came to me. It makes sense your answer and managed to cure my doubt. I will mark your reply as the correct answer to that post. Thank you very much!!!
– Eduardo Mendes