• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Choosing correct overloaded method (was useless: Didn't understand the answer)

 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Please check this.I have to overloaded methods like this..

----------------------------------------------------------------------------
public void method(Integer i){
System.out.println("Integer version");
}
public void method(String s){
System.out.println("String version");
}
----------------------------------------------------------------------------
And i'm invoking method like this:- method(null);

It gives me following error:-

reference to method is ambiguous, both method method(java.lang.String) in test and method method(java.lang.Integer) in test match
----------------------------------------------------------------------------

But , suppose i change methods like this

public void method(Integer i){
System.out.println("Integer version");
}
public void method(Object o){
System.out.println("Object version");
}
----------------------------------------------------------------------------

It compiles and gives output :- "Integer version"

I thought , it will be same compiler - error?Could you please explain , why it is so?
[ December 19, 2006: Message edited by: Barry Gaunt ]
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.xyzws.com/faq.do?cat=faq&article=8
[ December 19, 2006: Message edited by: Aniket Patil ]
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aniket for the link......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic