1

I have two interfaces, but they have a same method with different return value such as float and double. How can I implement both of them in a class, but not change the method name?

//Calculate.java
public interface Calculate {
    final float PI = 3.141592f;
    float getCircumference(float r);
    float getArea(float r);
}
interface GeometryShape{
    public static final float PI = 3.14159f;
    public abstract float getCircumference(float r);
    public abstract double getArea(float r);
    public abstract void draw();
}

//Circ.java
public class Circ implements  Calculate, GeometryShape{
    public float getCircumference(float r){
        return Calculate.PI * 2 * r;
    }

    public float getArea(float r){
        return Calculate.PI * r * r;
    }
Marvin Dev
  • 73
  • 1
  • 1
  • 7

3 Answers3

2

You can't implement both interfaces unless the methods differ in parameter types (or method name) when they return different types.

From JLS-8.1.5,

a class cannot have multiple methods with the same signature and different primitive return types (§8.4).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

What Elliot Frisch describes is true; only one method is allowed to have that particular method signature. A method signature is described by both the method name and its formal arguments - return type, visibility, whether or not it's static - that doesn't matter.

But, I'm going to make it easier and say that these two interfaces are virtually identical to each other, and only one really needs to exist.

When implementing an API, you can think of it like a bonafide contract that the implementors will have to follow, and the details of how it's implemented don't matter. To that end, I'd actually presume that GeometryShape could be seen as the base of all geometric shapes, and that would be suited as an abstract class instead.

From there, the interface would lose the constant declaration, and likely be turned into an adjective (Calculable, since all GeometricShapes are Calculable). Then you only have one place to deal with the signature mismatch.

Here's a sample.

interface Calculable {
    float getCircumference(float r);
    float getArea(float r);
}

abstract class GeometryShape implements Calculable {
    public static final float PI = 3.14159f;
    public abstract float getCircumference(float r);
    public abstract float getArea(float r);
    public abstract void draw();
}

I leave the rest as an exercise for the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • More generally, what Makoto is saying is that if you have two interfaces, both of which have a method with a certain signature (but differing return types), that's a sign that there needs to be some restructuring of the code. – Dennis Meng Oct 21 '14 at 05:20
  • I've got it.The next work to do is a subclass extends from GeometryShape. – Marvin Dev Oct 21 '14 at 07:11
0

Adding my two cents: you can implement two interfaces defining methods with the same signature only if the behaviour is the same for both: as there will be only one method implemented for both interfaces, it will be the same processing for both.

interface IName {
    int getID();
    String getName();
}
interface IValue {
    int getID();
    String getValue();
}

class Data implements IName, IValue {
    int id;
    String name, value;

    @Override
    public int getID() { // Obviously the same behaviour is expected for IName and IValue
        return id;
    }

    @Override
    public String getName() {
        return name;
   }

    @Override
    public String getValue() {
        return value;
   }
}

But you can't differentiate the implementation for both interface (though it would be nice, in future version of Java (public int IName.getID()?), as sometimes the interfaces you have to implement are not under your control).

Matthieu
  • 2,736
  • 4
  • 57
  • 87