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修飾字是最好的答案。