[英]Class C implements interface B which extends interface A, can you say C implements A?
In case class C implements interface B and interface B extends interface A. Is it correct to say that class C implements interface A?如果 class C 实现了接口 B 并且接口 B 扩展了接口 A。说 class Z0D61F8340CAD1D41257 实现接口是否正确?
Yes.是的。 C can override the methods of A.
C 可以覆盖 A 的方法。
Example: (It's not a good example, but it's just to show that C/Chimpanzee can override the methods of A/Animal)示例:(这不是一个很好的示例,但只是为了表明 C/Chimpanzee 可以覆盖 A/Animal 的方法)
interface Animal
{
void giveBirth();
}
interface Mammal extends Animal
{
void walk();
}
class Chimpanzee implements Mammal
{
@Override
public void giveBirth()
{
System.out.println("Chimpanzee gives birth.");
}
@Override
public void walk()
{
System.out.println("Chimpanzee walks.");
}
}
Even if I agree with the accepted answer with respect to the first part ("Yes."), I don't agree with the second part "C can override the methods of A".即使我同意关于第一部分的公认答案(“是”),我也不同意第二部分“C可以覆盖A 的方法”。
I believe the correct description is that C must implement the methods of both A and B.我相信正确的描述是C必须实现A和B的方法。
For example, if interface A declares the method fooA() and B declares the method fooB(), then C must implement both fooA() and fooB().例如,如果接口 A 声明方法 fooA() 而 B 声明方法 fooB(),则 C 必须同时实现 fooA() 和 fooB()。 In that sense, yes, you can say that C implements interface A (since it implements an interface that extends A).
从这个意义上说,是的,您可以说 C 实现了接口 A(因为它实现了扩展 A 的接口)。
The accepted answer is correct.接受的答案是正确的。 Because of polymorphism in the Java language, this is possible.
由于 Java 语言中的多态性,这是可能的。
However , one must understand the performance impact of using the JVM instruction
invokeDynamic
instead of invokeVirtual
.但是,必须了解使用 JVM 指令
invokeDynamic
而不是invokeVirtual
对性能的影响。 The latter on requires a simple lookup in the class's virtual table, whereas the former requires a much more in-depth recursive search through all of its implementor's virtual method tables.后者需要在类的虚拟表中进行简单的查找,而前者需要通过其所有实现者的虚拟方法表进行更深入的递归搜索。
Yes, you can say C implements A. The instanceof operator comes very handy here:是的,您可以说 C 实现了 A。instanceof 运算符在这里非常方便:
public interface B extends A {
public void b();
}
public interface A {
public void a();
}
public class C implements B {
@Override
public void a() {}
@Override
public void b() {}
public static void main(String[] args) {
C c = new C();
System.out.println("C is A? " + (c instanceof A));
System.out.println("C is B? " + (c instanceof B));
}
} }
The result will be:结果将是:
C is A? true
C is B? true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.