• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic