【发布时间】:2017-09-26 14:41:26
【问题描述】:
遇到有趣的继承/覆盖问题(取自 here)。
当基类和派生类都是抽象类时,并且
- 两者都有完全相同的抽象方法,或者
- abstract Base 有具体方法,Derived 声明 SAME 方法抽象。
我不明白为什么 Eclipse IDE 在 Derived 中调用它
- 实现(它在 Derived 中不提供代码/正文!)
- 覆盖(在 Derived 中也给出了 NO CODE / BODY!)
片段 1:
public abstract class Girl {
abstract String getDescription();
}
abstract class GirlDecorator extends Girl {
abstract String getDescription(); // implements Girl.getDescription() - says Eclipse IDE
}
片段 2:
public abstract class Girl {
String description = "no particular";
String getDescription() {
return description;
}
}
abstract class GirlDecorator extends Girl {
abstract String getDescription(); // overrides Girl.getDescription() - says Eclipse IDE
}
【问题讨论】:
-
您忘记了
@Override注释。 -
“Implements”是“overrides”的一个特例,它提供了一个实现。允许用抽象方法覆盖。
标签: java inheritance abstract-class overriding implements