Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking methods - help!!!!!

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you compile and run the following code?
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);
}
}
// Why does it print "My String"??
// Is it because it is the more specific method in
// the hierachy tree??
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Why does it print "My String"??
// Is it because it is the more specific method in
// the hierachy tree??

That's exactly that !
You might want to have a look at JLS 15.12.2 Compile-Time Step 2: Determine Method Signature
and
JLS 15.12 Method Invocation Expression in Plain English
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes String parameter is more specific than Object.
Read about the whole process at JLS 15.12
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. The JLS explains it well - cheers
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=012648
HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic