SCJP 1.6版考題 046

出自 陳富國維基館
前往: 導覽搜尋
10. public class Pizza{
11.   ArrayList toppings;
12.
13.   public final void addTopping(String topping){
14.     toppings.add(topping);
15.   }
16.
17.   public void removeTopping(String topping){
18.     toppings.remove(topping);
19.   }
20. }
And:
30. class PepperoniPizza extends Pizza{
31.   public void addTopping(String topping){
32.     System.out.println("Cannot add Toppings");
33.   }
34.
35.   public void removeTopping(String topping){
36.     System.out.println("Cannot remove pepperoni");
37.   }
38. }
And:
50. Pizza pizza = new PepperoniPizza();
51. pizza.addTopping("Mushrooms");
52. pizza.removeTopping("Pepperoni");


What is the result?
  A. Compilation fails.
  B. Cannot add Toppings
  C. The code runs with no output.
  D. A NullPointerException is thrown in Line 4.

解答


Ans: A

解說: 第13行,public final void addTopping,方法宣告成final表示子類別不能對這個方法進行覆載(overriding)