Java Method Overloading Resolution by Declared Type

Answer Correct answer: C — The program prints 'runsflips' because the compiler resolves overloaded methods using the declared types of the variables.

Given: What is the result? - image

  1. runsruns
  2. flipsruns
  3. runsflips Correct Answer
  4. Compilation fails.
  5. flipsflips

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.

Community Insight

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.

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.

Community Discussion (4 comments)

SrinivasJasti 👍 1 Selected: 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
9817c20 👍 2
B, compiled it in my brainz.
xplorerpj 👍 2 Selected: B
B is the correct answer. Tested in Intellij
Samps 👍 4 Selected: B
B is correct

Comments & Corrections

No comments 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.

Submitted for moderation before publishing. Keep it helpful and respectful.

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 →

Related Analysis

← Back to 1Z0-829 Study Guide