2009年2月9日 星期一

第46題

Given:
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.
答案:A
參考:6-5 繼承

-------------------------------------------------

31. public void addTopping(String topping) , try to override
13. public final void addTopping(String topping), which cannot be override coz of final

沒有留言:

張貼留言