*
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
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....?
 
Similar Threads
Static Method/Non-Static Method, RTTI
{} inside a method.
can "null" be considered as an object?
Shallow in static method ?
Overriding in Static Methods