0

I have a very basic question

Suppose we have two Interfaces i.e. I1, I2 and class C implementing both Interfaces

public interface I1 { public void m1(); } public interface I2 { public int m1(); } public class C implements I1,I2 { } 

Here how to resolve ambiguity issues?? Two methods with the same name having different return types

5
  • @luk2302 no, that is not related Commented Jul 15, 2021 at 7:18
  • @Rajat use meaningful names, and you won't have this. Commented Jul 15, 2021 at 7:19
  • 1
    @Stultuske it is a perfect duplicate, the top answer answers the entire question perfectly, with examples that work and that do not work and explain why. Commented Jul 15, 2021 at 7:24
  • @Stultuske "If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error. This is the general rule [...] and applies also to possible conflicts not only between 2 inherited interface methods, but also an interface and a super class method, or even just conflicts due to type erasure of generics." Commented Jul 15, 2021 at 7:24
  • Solution: either make the method names different,make the number of arguments different or make the types of the arguments different. Commented Jul 15, 2021 at 7:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.