• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading methods

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After running following code,I am getting result as given below:
Here from String!!null
Why does it call String instead of Object?When I passed null directly through the method.

Code is given below:
import java.util.*;
class OverLoadinObject
{
void aMethod1(Object obj)
{
System.out.println("Here from object!!"+obj);}

void aMethod1(String str)
{
System.out.println("Here from String!!"+str );
}
public static void main(String[] args)
{
OverLoadinObject objTestOverLoadinObject = new OverLoadinObject();

objTestOverLoadinObject.aMethod1(null);
}
}
When I passed null like following way ,the Object method gets called.
Object obj = null;
objTestOverLoadinObject.aMethod1(obj);
Here from object!!null
Here it is being called Object overloaded method,why does it behave like this? :roll:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic