• 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

overloading

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test
{
public void myMethod(Object o)
{
System.out.println("My Object");
}

public void myMethod(String s)
{
System.out.println("My String");
}
public static void main(String args[])
{
test t = new test();
t.myMethod(null);
}
}
r there any other class accept "null" ?
[ August 06, 2003: Message edited by: Kefe Abalov ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wot you mean to ask.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
going up from the hierarchy, a String object is found first than Object, which is the parent for all classes. that's the reason public void myMethod(String s) is invoked.
In other words
public void myMethod(String s)
is more specific than
public void myMethod(Object o)
let me know if I guessed your question
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't think there's any other value other than null....but why is it that always "My String" is printed though Object comes first in hierarchy than the String object??
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but why is it that always "My String" is printed though Object comes first in hierarchy than the String object??
because String is the first that finds going from bottom to top.

It doesn't start from Object downwards. viceversa.
 
Kefe Abalov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

going up from the hierarchy, a String object is found first than Object, which is the parent for all classes. that's the reason public void myMethod(String s) is invoked.


Andres Gonzalez thanks for ur explanation though i didn't ask clearly
and if i rewrite the code and replace the "Object" with "StringBuffer",just like this below
public class test
{
public void myMethod(StringBuffer o)
{
System.out.println("My Object");
}
public void myMethod(String s)
{
System.out.println("My String");
}
public static void main(String args[])
{
test t = new test();
t.myMethod(null);
}
}
it won't compile since the StringBuffer and String are in the same hierarchy and the compiler can't decide which method should invoke.
[ August 07, 2003: Message edited by: Kefe Abalov ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it won't compile since the StringBuffer and String is in the same hierarchy and the compilor can't decide which method should invoke
Correcto
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic