我即将参加 Java SE 8 程序员 I 考试 (1Z0-808)。我正在使用本学习指南:https://www.selikoff.net/java-oca-8-programmer-i-study-guide/ .在回答第 5 章(类(class)设计)的复习题时,我没有答对这道题:
以下关于具体子类的说法中哪一项是正确的? (选择所有适用项)
- 具体的子类可以声明为抽象的。
- 一个具体的子类必须实现所有继承的抽象方法。
- 一个具体的子类必须实现定义在一个类中的所有方法 继承接口(interface)。
- 具体子类不能标记为 final。
- 抽象方法不能被具体子类覆盖。
我的答案是 2 和 5。但只有第 2 个是正确的。我选择了第 5 个答案,因为我认为你确实不能从抽象类覆盖抽象方法,但你可以实现它,就像 inferfaces,它几乎像 Java 8 以来的抽象类。
知道接口(interface)抽象方法是实现的,而不是重写的,当谈论抽象类时:“抽象方法可以被具体子类重写”而不是“抽象方法可以被具体子类实现”是否正确?
如果我们注意第二个答案(这是正确的),他们使用了“实现”一词。
最佳答案
这就是the specification使用这个术语:
If a non-
abstract
methodm<sub>C</sub>
overrides an abstract methodm<sub>A</sub>
from a classC
, thenm<sub>C</sub>
is said to implementm<sub>A</sub>
fromC
.An instance method
m<sub>C</sub>
declared in or inherited by classC
, overrides fromC
another methodm<sub>I</sub>
declared in an interfaceI
, iff all of the following are true: [...]
请注意,它确实使用术语“覆盖” 来指代覆盖接口(interface)方法。
更简单地说,如果一个方法重写了一个抽象方法(来自抽象类或接口(interface)),那么重写方法实现抽象方法。不过,它仍被视为覆盖。
abstract class A {
abstract void m();
void n() {}
}
class C extends A {
// C.m() both overrides and implements A.m()
@Override
void m() {}
// C.n() overrides A.n(), but does not implement it
@Override
void n() {}
}
关于java - 抽象方法实现与抽象方法覆盖。这两个对于抽象类意味着相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44856645/