Given the code fragment:
abstract class Planet {
protected void revolve() { // line n1
}
abstract void rotate(); // line n2
}
class Earth extends Planet {
void revolve() { // line n3
}
protected void rotate() { // line n4
}
}
Which two modifications, made independently, enable the code to compile?
A. Make the method at line n1 public.
B. Make the method at line n2 public.
C. Make the method at line n3 public.
D. Make the method at line n3 protected.
E. Make the method at line n4 public.
題解
題目原本提供的程式會在 line n3 編譯錯誤,原因在於 Earth 類別繼承的 Planet 類別,其 revolve 方法的可見度使用 protected 來修飾。若要在 Earth 類別覆寫 revolve 方法,其可見度必須不能小於 protected,因此 Earth 類別的 revolve 方法的可見度只能用 protected 或是 public 來修飾。
