[英]Inheritance and Interface
I've been trying to understand inheritance when interfaces are involved. 当涉及接口时,我一直在尝试理解继承。 I want to know how the subclasses are created if they follow the following:
我想知道子类是如何创建的,如果它们遵循以下内容:
For Example, let's say i have: 例如,假设我有:
My Questions 我的问题
Do i have to provide the implementation of the interface methods 'q and r' in all of the subclasses which extend A? 我是否必须在扩展A的所有子类中提供接口方法'q和r'的实现?
If i don't provide the implementation of the interface in a subclass will i have to make that subclass an Abstract Class? 如果我不在子类中提供接口的实现,我是否必须使该子类成为抽象类?
Is it possible for any of the subclass to implement I? 任何子类都可以实现我吗? eg Class C extends A implements I, is this possible?
例如,C类扩展A实现I,这可能吗? even though it's already extending a superclass which implements I?
即使它已经扩展了一个实现我的超类?
Let's say i don't provide the implementation of method r from the interface I, then i will have to make the superclass A and Abstract class! 假设我没有从接口I提供方法r的实现,那么我将不得不制作超类A和抽象类! is that correct?
那是对的吗?
My example code: 我的示例代码:
//superclass
public class A implements I{
x(){System.out.println("superclass x");}
y(){System.out.println("superclass y");}
q(){System.out.println("interface method q");}
r(){System.out.println("interface method r");}
}
//Interface
public Interface I{
public void q();
public void r();
}
//subclass 1
public class B extends A{
//will i have to implement the method q and r?
x(){System.out.println("called method x in B");}
y(){System.out.println("called method y in B");}
}
//subclass 2
public class C extends A{
//will i have to implement the method q and r?
x(){System.out.println("called method x in C");}
y(){System.out.println("called method y in C");}
}
1) No, you do not need to implement the methods in the subclasses, because they are already defined in the superclass. 1)不,您不需要在子类中实现这些方法,因为它们已经在超类中定义。 The subclass will inherit those method definitons.
子类将继承那些方法定义。
2) No, see 1. The only exception is if the superclass is abstract and doesn't implement the interface, then you will need to implement it in the subclass if the subclass is not abstract. 2)不,请参阅1.唯一的例外是如果超类是抽象的并且没有实现接口,那么如果子类不是抽象的,则需要在子类中实现它。
3) No. It might compile properly, but will have no effect, and so shouldn't be done. 3)不可以。它可以正确编译,但不起作用,所以不应该这样做。
4) Yes, this is correct. 4)是的,这是正确的。 If you do not implement a method from the interface, you need to make the class abstract.
如果未从接口实现方法,则需要使类成为抽象。
Only an abstract-class can keep them abstract, meaning an abstract-class is not required to provide an implementations for the methods in the interface. 只有抽象类可以使它们保持抽象,这意味着不需要抽象类来为接口中的方法提供实现。
Since, A
is concrete it must provide the implementations. 因为
A
是具体的,它必须提供实现。 Then the subclasses of A
will just inherit those implementation from A
. 然后
A
的子类将从A
继承这些实现。
But, if A
was abstract and didn't provide implementations for the methods, then B
and C
would have to provide the implementations. 但是,如果
A
是抽象的并且没有提供方法的实现,那么B
和C
必须提供实现。
1: NO, if you implemnt them in your superclass,its not required to implement them in your subclasses 1:不,如果您在超类中实现它们,则不需要在子类中实现它们
2: If you dontimplement the methods in your Superclass then you havetomake it abstract and then make your concrete subclasses implement those methods 2:如果你没有实现超类中的方法,那么你必须将它抽象为抽象,然后使你的具体子类实现这些方法
3: yes, but absolutely redundant as your superclass is already implementing thrm. 3:是的,但绝对多余,因为你的超类已经在实现thrm。
4: yep, and you should implement those methods in the class when extends your superclass 4:是的,你应该在扩展你的超类时在类中实现这些方法
An interface is a promise to the outside world that "I can provide these methods." 界面是对外界的承诺,“我可以提供这些方法”。
1) and 2) and 4) Since superclass A already implements interface I, it has promised the outside world. 1)和2)和4)由于超类A已经实现了接口I,它已经向外界承诺了。 Superclass A can fulfil that promise by:
超级A可以通过以下方式实现这一承诺:
3) All the subclasses of the superclass A already implement I because they inherit the "promise", so Class C extends A implements I is redundant. 3)超类A的所有子类已经实现了I,因为它们继承了“promise”,所以C类扩展了A实现我是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.