Java Method Overloading Resolution by Declared Type

1Z0-829 By islever Editorial Last updated

Short answer

C The program prints 'runsflips' because the compiler resolves overloaded methods using the declared types of the variables.

Question

Given: What is the result? - Exhibit: Java Method Overloading Resolution by Declared Type
  1. runsruns
  2. flipsruns
  3. runsflips Correct Answer
  4. Compilation fails.
  5. flipsflips

Key Takeaways

The question tests static binding for overloading; the common trap is assuming dynamic dispatch (runtime polymorphism) applies to overloaded methods rather than overridden ones.

This Java 17 exam question tests the rule that method overloading is resolved at compile time based on the declared type of arguments, not their runtime types. The correct answer is B because the compiler binds calls to Animal and Dog signatures respectively.

Common mistake: Learners often choose C or E, mistakenly applying runtime polymorphism rules where they do not apply, leading them to believe the actual object instance determines which overloaded method is called.

Expert Analysis

Why the Answer Is Correct

The code demonstrates method overloading, where multiple methods share the same name but have different parameter types. In Java, the decision of which overloaded method to invoke is made at compile time based strictly on the declared type of the variable passed as an argument. Since the variables are declared as Animal and Dog, the compiler selects the runs(Animal) and runs(Dog) versions respectively, producing "runsflips".

Why the Other Options Are Wrong

Option A is incorrect because it assumes both calls resolve to the base class version. Option C is a common distractor resulting from confusing overloading with overriding. Option D is wrong because the code is syntactically valid. Option E would only be correct if both arguments were treated as the most specific subclass, which violates Java's overload resolution rules.

Community Comment Notes

The community consensus correctly identifies B as the answer. As user SrinivasJasti noted, "The compiler decides which method to call based solely on the declared type... This applies to both static and instance methods." User xplorerpj confirmed this by testing in IntelliJ, validating that the output matches the theoretical explanation.

Exam Strategy

Always distinguish between Overloading (compile-time/dispatched by signature) and Overriding (runtime/dispatched by object type). If the method signature differs, look at the variable declaration. If the signature is identical, look at the object instance.

Frequently Asked Questions

Does the runtime type matter for overloaded methods?

No. Overloaded methods are resolved at compile time based on the declared parameter types, regardless of the actual object instance.

Why is 'Compilation fails' incorrect here?

The code is valid Java. The compiler can clearly distinguish between the two runs methods by their argument types Animal and Dog.

More 1Z0-829 FAQ →

Community Votes

B
100%

100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 4 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 4

  1. xplorerpj 👍 2 Answered B
    B is the correct answer. Tested in Intellij
  2. SrinivasJasti 👍 1 Answered B
    The compiler decides which method to call based solely on the declared type of the variable passed as an argument, not the runtime type of the object it refers to. This applies to both static and instance methods.Here declared type is Animal and Dog respectively
  3. Samps 👍 4 Answered B
    B is correct
  4. 9817c20 👍 2
    B, compiled it in my brainz.

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to 1Z0-829 Study Guide