Home >Java >javaTutorial >How Does Java Method Overloading Handle Real Parameter Types vs. Declared Types?
Overloaded Method Invocation: Prioritizing Real Parameter Types
Java's method overloading mechanism selects the most appropriate implementation based on the declared parameter types. However, a common misconception is that it also considers the actual types during overload resolution.
The Challenge
Consider the following code snippet:
interface Callee { public void foo(Object o); public void foo(String s); public void foo(Integer i); } class CalleeImpl implements Callee { public void foo(Object o) { logger.debug("foo(Object o)"); } public void foo(String s) { logger.debug("foo(\"" + s + "\")"); } public void foo(Integer i) { logger.debug("foo(" + i + ")"); } } Callee callee = new CalleeImpl(); Object i = new Integer(12); Object s = "foobar"; Object o = new Object(); callee.foo(i); callee.foo(s); callee.foo(o);
Upon execution, this code unexpectedly prints "foo(Object o)" three times, rather than selecting different implementations based on the real parameter types.
The Root Cause
In Java, method invocation dispatches dynamically for the object the method is called on, but not for the parameter types. The Java Language Specification explicitly states that the "compile-time types of the arguments" determine the selected method signature.
Therefore, the parameter declarations override any potential type conversions or promotions that occur during parameter assignment. As a result, all three calls in the provided code resolve to the "foo(Object o)" method since Object is the declared type of all three parameters.
Resolution
To resolve this issue and prioritize real parameter types in overload resolution, consider the following strategies:
The above is the detailed content of How Does Java Method Overloading Handle Real Parameter Types vs. Declared Types?. For more information, please follow other related articles on the PHP Chinese website!