Given:
public final class IceCream {
public void prepare() {
}
}
public class Cake {
public final void bake(int min, int temp) {
}
public void mix() {
}
}
public class Shop {
private Cake c = new Cake();
private final double discount = 0.25;
public void makeReady() {
c.bake(10, 120);
}
}
public class Bread extends Cake {
public void bake(int minutes, int temperature) {
}
public void addToppings() {
}
}
Which statement is true?
A. A compilation error occurs in IceCream.
B. A compilation error occurs in Cake.
C. A compilation error occurs in Shop.
D. A compilation error occurs in Bread
E. All classes compile successfully.
題解
Bread.java第三行的bake方法會編譯錯誤,因為Bread類別繼承的Cake類別,其bake方法已經使用了final來修飾,無法被覆寫。