Multiple choice technology programming languages

What is the result of the following code? 1. public abstract class Catchable { 2. protected abstract void catchAnObject(Object x); 3. 4. public static void main(String [] args) { 5. java.util.Date now = new java.util.Date(); 6. Catchable target = new MyStringCatcher(); 7. target.catchAnObject(now); 8. } 9. } 10. 11. class MyStringCatcher extends Catchable { 12. public void catchAnObject(Object x) { 13. System.out.println("Caught object"); 14. } 15. 16. public void catchAnObject(String s) { 17. System.out.println("Caught string"); 18. } 19. }

  1. Caught string

  2. Caught object

  3. Compiler error on line 12

  4. Compiler error on line 2

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The reference variable target is of type Catchable. When calling target.catchAnObject(now), polymorphism and overload resolution apply. Overload resolution happens at compile-time based on the static type. Since now is a Date object, it matches catchAnObject(Object) at compile time and runs the overridden method in MyStringCatcher printing 'Caught object'.