• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic