Which of the following can fill in the blank in this code to make it compile?
public class Exam {
void method() {
}
}
public class OCAJP extends Exam{
_____ void method(){}
}
A. abstract
B. final
C. private
D. default
E. int
题解
在 Java 中,子类别若要覆写父类别的方法,其方法的可见度只能等同或是大于父类别的方法。因此在要覆写的方法原本没有使用可见度修饰字(default)时,覆写时只能同样不使用可见度修饰字或是可见度范围更高的 protected 或是 public。但在这题目中,并没有适合的可见度选项能用,因此应该要保留不使用可见度修饰字。
所以选项 B,使用让方法不再能被覆写的 final 修饰字是最好的答案。
