What is the result?

Given:

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?

Given:

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.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


Matti

Matti

E is correct, Dog is never a Cat, there is no is-a-relation and it is just known in runtime.