Oracle
1Z0-851 · Question #36
Given: class Animal { public String noise() { return "peep"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return…
The correct answer is E. An exception is thrown at runtime. Exception in thread "main" java.lang.ClassCastException: Dog cannot be cast to Cat at Client.main(Client.java:12)
Analyzing and Troubleshooting System Problems
Question
Given: class Animal { public String noise() { return "peep"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } 30. Animal animal = new Dog(); 31. Cat cat = (Cat)animal; 32. System.out.println(cat.noise()); What is the result?
Options
- Apeep
- Bbark
- Cmeow
- DCompilation fails.
- EAn exception is thrown at runtime.
How the community answered
(27 responses)- A11% (3)
- B4% (1)
- C4% (1)
- D4% (1)
- E78% (21)
Explanation
Exception in thread "main" java.lang.ClassCastException: Dog cannot be cast to Cat at Client.main(Client.java:12)
Topics
#Java casting#class hierarchy#ClassCastException#polymorphism
Community Discussion
No community discussion yet for this question.