| Author |
overloading method output need explanation
|
Athira Vineeth
Greenhorn
Joined: Sep 06, 2010
Posts: 16
|
|
Hi
in the below example
The output of this program is "String".
Why so?Why it is not executing the printValue(Object ob) method?
|
 |
Panagiotis Kalogeropoulos
Rancher
Joined: May 27, 2011
Posts: 99
|
|
Although both methods are applicable to your situation (both can accept null as a parameter), the most specific method will be called, or in other words, the method that is "closer" to the argument that you specify. Since a parameter in printValue(String ob) can be passed to printValue(Object ob) also, but not vice versa, then the printValue(String ob) is more specific and that is why it is called. The same rule applies to primitives too. For instance, check the following code:
The most specific method is called depending on the arguments.
Hope this helps!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
It’s in the Java Language Specification (JLS); look for the links to ยง15.12, too. Note this is a complicated subject, even by the standards of the JLS.
|
 |
Athira Vineeth
Greenhorn
Joined: Sep 06, 2010
Posts: 16
|
|
yes its help me a lot.
thank you so much for your time.
|
 |
 |
|
|
subject: overloading method output need explanation
|
|
|