• Post Reply Bookmark Topic Watch Topic
  • New Topic

overloading method output need explanation  RSS feed

 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yes its help me a lot.

thank you so much for your time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!