java 继承和接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13131592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheritance and Interface
提问by Jon Snow
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:
例如,假设我有:
- a Superclass which implements an Interface I
- and couple of subclasses which extend the superclass A
- 实现接口 I 的超类
- 以及扩展超类 A 的几个子类
My Questions
我的问题
Do i have to provide the implementation of the interface methods 'q and r' in all of the subclasses which extend A?
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? e.g. Class C extends A implements I, is this possible? 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! is that correct?
我是否必须在扩展 A 的所有子类中提供接口方法“q 和 r”的实现?
如果我不在子类中提供接口的实现,我是否必须使该子类成为抽象类?
任何子类都可以实现 I 吗?例如 C 类扩展 A 实现 I,这可能吗?即使它已经扩展了一个实现 I 的超类?
假设我没有从接口 I 提供方法 r 的实现,那么我将不得不创建超类 A 和抽象类!那是对的吗?
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");}
}
采纳答案by Jonathon Ashworth
1) No, you do not need to implement the methods in the subclasses, because they are already defined in the superclass. The subclass will inherit those method definitons.
1) 不,您不需要在子类中实现方法,因为它们已经在超类中定义。子类将继承这些方法定义。
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. If you do not implement a method from the interface, you need to make the class abstract.
4) 是的,这是正确的。如果不从接口实现方法,则需要使类抽象。
回答by Bhesh Gurung
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. Then the subclasses of A
will just inherit those implementation from A
.
由于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
必须提供实现。
回答by PermGenError
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:是的,你应该在扩展超类时在类中实现这些方法
回答by Kiwi
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. Superclass A can fulfil that promise by:
1)和2)和4)由于超类A已经实现了接口I,所以对外承诺了。超类 A 可以通过以下方式实现这一承诺:
- implementing the method - In this case, your subclass has already inherited that method and doesn't need to implement anything.
- Declaring itself abstract - In this case, your subclass must either implement the abstract method, or declare itself abstract too and "pass the buck" down to any class extending the subclass.
- 实现方法 - 在这种情况下,您的子类已经继承了该方法并且不需要实现任何东西。
- 声明自己是抽象的——在这种情况下,你的子类必须要么实现抽象方法,要么也声明自己是抽象的,并将“推卸责任”给任何扩展子类的类。
3) All the subclasses of the superclass A alreadyimplement I because they inherit the "promise", so Class C extends A implements I is redundant.
3)超类A的所有子类都已经实现了I,因为它们继承了“promise”,所以C类继承A实现了I是多余的。