File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding passing null into method parameter

 
Dhruv Arya
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to know which gets invoked ?? I tried it out the string version does but how ? and also if we remove the string version the Integer Version gets invoked and removing that then the Object Version so how do we know which to choose?
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I complied your code and it throws the error like "reference to method is ambiguous, both method method(String s) and method(Integer s)".

The rule is something like this:
The method which is nearest to the parameter is called first, means, the child class object is first checked, then its parent, and then its parent and so on..
e.g

If we comment out the "integer version", then it looks for parent class
e.g


HTH
 
Dhruv Arya
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot I got what you were trying to say.. Thanks alot !
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome I saw this type of questions many time here. Just search this forum with tags like "method calling sequence", "which method get invoked". etc
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there is a important thing to remember. If you have overloaded methods with parameters in the same inheritance tree then the lowest level subclass will be invoked. If any other type is included then it will be ambiguous as both the types can accept the argument value.
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish Bangera wrote:If you have overloaded methods with parameters in the same inheritance tree then the lowest level subclass will be invoked.

I think, this is more technical version of what I want to say
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic