This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes why is String method called not Object method....? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why is String method called not Object method....?" Watch "why is String method called not Object method....?" New topic
Author

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

srivijay varma
Greenhorn

Joined: Nov 15, 2006
Posts: 5
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

Joined: Apr 24, 2004
Posts: 23
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is String method called not Object method....?