[英]Two interface have same method name with different return type
This question is for understanding interfaces in Java. 这个问题是为了理解Java中的接口。 Here is a very simple example of implementing a interfaces in Java. 这是在Java中实现接口的一个非常简单的示例。
interface ParentA {
void display();
}
interface ParentB {
int display();
}
class Child implements ParentA, ParentB {
@Override
public void display() {
System.err.println("Child ParentA");
}
//ERROR : The return type is incompatible with ParentB.display()
//So added method with int return type too
@Override
public int display() {
System.err.println("Child ParentB");
}
}
This case can happen in large Java application where two interface can have method with same name. 这种情况可能发生在大型Java应用程序中,其中两个接口可以具有相同名称的方法。 I thought that since return type is different JVM will know which interface's method we are overriding. 我认为,由于返回类型不同,JVM将知道我们覆盖哪个接口的方法。
What is the best explanation for this? 对此最好的解释是什么? Does this situation make sense? 这种情况有意义吗?
Thanks in Advance 提前致谢
Because method with same signature is not allowed, It confuses compiler to detect the exact override-equivalent method from the declared once. 因为不允许使用具有相同签名的方法,所以它会混淆编译器以从声明的一次检测精确的覆盖等效方法。
JLS (§8.4.2) JLS(§8.4.2)
Two methods or constructors, M and N, have the same signature if they have, 两个方法或构造函数M和N如果有,则具有相同的签名,
- the same name 同名
- the same type parameters (if any) ( §8.4.4 ), and 相同的类型参数(如果有的话)( §8.4.4 ),和
- after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types. 在将N的形式参数类型调整为M的类型参数之后,使用相同的形式参数类型。
It is a compile-time error to declare two methods with override-equivalent signatures in a class. 在类中声明具有覆盖等效签名的两个方法是编译时错误。
If you found yourself in this situation and couldn't solve in a cleaner way, you could have one or two inner classes that forward the calls to newly named methods: 如果您发现自己处于这种情况并且无法以更清晰的方式解决,则可以使用一个或两个内部类来转发对新命名方法的调用:
class Child {
private class ParentAImp implements ParentA {
@Override
public void display() {
displayParentA();
}
}
private class ParentBImp implements ParentB {
@Override
public int display() {
return displayParentB();
}
}
public ParentA asParentA(){ return new ParentAImp(); }
public ParentB asParentB(){ return new ParentBImp(); }
private void displayParentA() {
System.err.println("Child ParentA");
}
private int displayParentB() {
System.err.println("Child ParentB");
return 0;
}
}
Drawback is now to get from Child
to interface you have to do: 现在回顾从Child
到接口你需要做的事情:
ParentA parentA = child.asParentA();
ParentB parentB = child.asParentB();
It is not possible to have in the same class two methods with the same signature and different return type. 在同一个类中不可能有两个具有相同签名和不同返回类型的方法。
Definition of signature: 签字的定义:
The combination of the method name and the parameter list. 方法名称和参数列表的组合。
This happens because it is not possible for the jvm two choose between to methods that differs only from the return type. 发生这种情况是因为jvm两者不可能在仅与返回类型不同的方法之间进行选择。 The jvm infact can investigate only the name of the method and the type of parameters of the call. jvm infact只能调查方法的名称和调用的参数类型。
So your example is not possible in java. 所以你的例子在java中是不可能的。
Probably you have to create two classes, one for interface. 可能你必须创建两个类,一个用于接口。
To implement overriding you are breaking overloading rules. 要实现覆盖,您将打破重载规则。
The conditions for method overloading 方法重载的条件
So in your case, you can not have same method name having same method arguments (in your case no arguments) and different return type. 因此,在您的情况下,您不能使用相同的方法名称具有相同的方法参数(在您的情况下没有参数)和不同的返回类型。
If you add a parameter to one of your interface method then your code will compile. 如果您向其中一个接口方法添加参数,那么您的代码将被编译。
interface ParentA {
void display();
}
interface ParentB {
int display(int x);
}
class Child implements ParentA, ParentB {
@Override
public void display() {
System.err.println("Child ParentA");
}
@Override
public int display(int x) {
System.err.println("Child ParentB");
return 0;
}
}
If you think about it from the side of some code calling methods on an instance of Child: 如果你从一个代码调用Child的一个实例上的方法来考虑它:
Child child = new Child();
child.display();
Which display method would be called? 将调用哪种显示方法? Seeing as it could be either method, you'd see a compile time error. 看到它可能是任何一种方法,您会看到编译时错误。
An interesting way round this in Java 8 would be to use a default method in one of the interfaces; 在Java 8中使用一种有趣的方法是在其中一个接口中使用默认方法; these come with their own set of rules. 这些都有自己的一套规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.