Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is String method called not Object method....?

 
srivijay varma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.myTest(null);
}
public void myTest(Object o)
{
System.out.println("Inside the Object Method");
}
public void myTest(String s)
{
System.out.println("Inside the String Method");
}
}
 
Krishnan Loganathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Varma,

When you invoke a overloaded method, the compiler will try to invoke the method, from which we can invoke the other overloaded method. (ie., from that method, it can invoke the other method with the same name, in this case, from the method public void myTest(String s), we can invoke the other method public void myTest(Object o))

Due to this, it is invoking public void myTest(String s). If you declare one more method as follows means,

public void myTest(StringBuffer strBuffer) {
}

compiler will throw error, because compiler will get confused, and it doesn't know to invoke which method.


Regards,
Loga
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic