SCJP 1.6版考題 085

出自 陳富國維基館
前往: 導覽搜尋
11. class Animal{public String noise(){return "peep";}}
12. class Dog extends Animal{
13.   public String noise(){return "bark";}
14  }
15. class Cat extends Animal{
16.   public String noise(){return "meow";}
17. }
...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());


What is the result?
  A. peep
  B. bark
  C. meow
  D. Compilation fails.
  E. An exception is thrown at runtime.

解答


Ans: E

解說:

animal 是指向Dog物件,
Cat類別與Dog類別二者間無繼承關係,不可以指向animal 所指向的Dog物件