nerdexam
Oracle

1Z0-819 · Question #24

Given: public class Foo { public void foo(Collection arg) { System.out.println("Bonjour le monde!"); } } and public class Bar extends Foo { public void foo(Collection arg) {…

The correct answer is D. b1.foo(c) prints Hello world! H. f2.foo(c) prints Hello world! I. f1.foo(c) prints Bonjour le monde! This question tests two core Java concepts simultaneously: runtime polymorphism (overriding) and compile-time overload resolution (overloading). Why D, H, and I are correct: I (f1.foo(c) → "Bonjour le monde!"): f1 is declared and actually instantiated as Foo, so it calls…

Java Object-Oriented Approach

Question

Given: public class Foo { public void foo(Collection arg) { System.out.println("Bonjour le monde!"); } } and public class Bar extends Foo { public void foo(Collection arg) { System.out.println("Hello world!"); } public void foo(List arg) { System.out.println("Hola Mundo!"); } } and Foo f1 = new Foo(); Foo f2 = new Bar(); Bar b1 = new Bar(); Collection<String> c = new ArrayList<>(); Which three are true? (Choose three.)

Options

  • Ab1.foo(c) prints Bonjour le monde!
  • Bf1.foo(c) pr ts Hello world!
  • Cf1.foo(c) prints Hola Mundo!
  • Db1.foo(c) prints Hello world!
  • Eb1.foo(c) prints Hola Mundo!
  • Fb1.foo(c) prints Olá Mundo!
  • Gf2.foo(c) prints Bonjour le monde!
  • Hf2.foo(c) prints Hello world!
  • If1.foo(c) prints Bonjour le monde!

How the community answered

(26 responses)
  • A
    8% (2)
  • C
    4% (1)
  • D
    73% (19)
  • G
    15% (4)

Explanation

This question tests two core Java concepts simultaneously: runtime polymorphism (overriding) and compile-time overload resolution (overloading).

Why D, H, and I are correct:

  • I (f1.foo(c) → "Bonjour le monde!"): f1 is declared and actually instantiated as Foo, so it calls Foo.foo(Collection) - straightforward.
  • H (f2.foo(c) → "Hello world!"): f2 is declared as Foo but instantiated as Bar. At compile time, the compiler sees Foo's foo(Collection) signature. At runtime, dynamic dispatch kicks in and executes Bar's overriding version, printing "Hello world!".
  • D (b1.foo(c) → "Hello world!"): b1 is Bar, which has both foo(Collection) and foo(List). The argument c is declared as Collection<String> - its compile-time type is Collection, not List. Since Collection does not satisfy List, the compiler selects foo(Collection), printing "Hello world!".

Why the distractors fail:

  • A/G are wrong because Bar overrides foo(Collection), so "Bonjour le monde!" never prints for any Bar instance (or Foo reference holding a Bar).
  • B/C are wrong because f1 is genuinely a Foo object with no overriding - it always prints "Bonjour le monde!".
  • E is the key trap: even though c is actually an ArrayList (which implements List), overload selection happens at compile time using the declared type Collection. Foo(List) is never chosen, so "Hola Mundo!" never prints.
  • F is a distractor - no such method exists anywhere.

Memory tip: Remember "override = runtime, overload = compile time." When you see a method call, ask two questions in order: (1) Which overload does the compiler pick based on the declared argument types? (2) Which override does the JVM execute based on the actual object type?

Topics

#Method Overloading#Method Overriding#Polymorphism#Dynamic Dispatch

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice