Java Method Overloading Resolution by Declared Type
Short answer
C The program prints 'runsflips' because the compiler resolves overloaded methods using the declared types of the variables.
Question

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?
Why is 'Compilation fails' incorrect here?
runs methods by their argument types Animal and Dog.Community Votes
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
-
xplorerpj 👍 2 Answered BB is the correct answer. Tested in Intellij
-
SrinivasJasti 👍 1 Answered BThe 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
-
Samps 👍 4 Answered BB is correct
-
9817c20 👍 2B, compiled it in my brainz.
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.