21

Possible Duplicate:
Method name collision in interface implementation - Java

What do we do if we need to implement two interfaces both of which contain a method with the same name and parameters, but different return types? For example:

interface A {
    public int foo();
}

interface B {
    public double foo();
}

class C implements A, B {
    public int foo() {...}  // compilation error
}

Is there an easy way to overcome this issue?

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 3
    Why would you have two interfaces like that in the first place?? Or are you getting it from somewhere else? – Rohit Jain Oct 08 '12 at 13:13
  • 1
    Use `Facade` pattern to enclose invokation of one equal-named method? As you can't declare two methods with equal names(even with diffirent return type) within one class. – Yegoshin Maxim Oct 08 '12 at 13:17

6 Answers6

14

The simplest solution is to always return double in A as it can store every possible int value.

If you is not an option you need to use an alternative to inheritance.

class C {
    public A getA();
    public B getB();
}

C c = new C();
int a = c.getA().foo();
double b = c.getB().foo();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • what if return type is an object(any) and int? we can not do same. – Nandkumar Tekale Oct 08 '12 at 13:20
  • 2
    Simply returning double won't solve the problem... There will still be a compiler error. You would also need to change the return type of `foo()` in A (which might not be possible if A was a previously existing interface). Your other proposed solution is probably better. – Alderath Oct 08 '12 at 13:23
  • @NandkumarTekale You have to find a super type for both methods. Or use the second approach. – Peter Lawrey Oct 08 '12 at 13:29
  • Another idea: Using Number instead of double. – Grim Dec 09 '12 at 07:57
13

You cant. Java uniquely identifies methods by their name and their parameters, not their return type.

Samuel
  • 16,923
  • 6
  • 62
  • 75
4

You can write an Adapter class to implement one of the interfaces.

Example implementation:

class AdapterA implements A{
     AdapterA(C c){impl = c;}
     private final C impl;
     public int foo(){return c.fooReturningInt();}
}
class C implements B{

   public double foo(){...}
   public int fooReturningInt(){...}
}
josefx
  • 15,506
  • 6
  • 38
  • 63
3

Use Number instead of double and int in interface A and B.

Grim
  • 1,938
  • 10
  • 56
  • 123
3

A method in Java is uniquely defined by its signature. From http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

maba
  • 47,113
  • 10
  • 108
  • 118
manub
  • 3,990
  • 2
  • 24
  • 33
  • Actually, same name and argument with different return type are illegal in java code, but legal in bytecode. – Matthieu Sep 19 '17 at 14:11
2

Your foo() method is clearly not well defined. Likely there should be a parent interface with a public Number foo(), which is extended by A and B who override that to a more specific type. There isn't really a sensible way your class can implement both those interfaces unless you rename one of the foo methods.

Vala
  • 5,628
  • 1
  • 29
  • 55