6.11. Answers to sample exam questions
What is the output of the following code?
class Animal { void jump() { System.out.println("Animal"); } } class Cat extends Animal { void jump(int a) { System.out.println("Cat"); } } class Rabbit extends Animal { void jump() { System.out.println("Rabbit"); } } class Circus { public static void main(String args[]) { Animal cat = new Cat(); Rabbit rabbit = new Rabbit(); cat.jump(); rabbit.jump(); } }
Animal Rabbit Cat Rabbit Animal Animal- None of the above
Answer: a
Explanation: Although the classes Cat and Rabbit seem to override the method jump, the class Cat doesn’t override the method jump() defined in the class Animal. The class Cat defines a method parameter with the method ...
Get OCA Java SE 8 Programmer I Certification Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.