Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

bit interesting....  RSS feed

 
yogesh gargate
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the program...

public class simple
{
public void display(Object obg)
{
System.out.println("in display with OBJECT argument");
}
public void display(String str)
{
System.out.println("in display with STRING augument");
}
public static void main(String [] args)
{
simple s = new simple();
s.display(null);
}
}

While executing it gives output...
"in display with STRING augument"

Why?
Actually i was expecting display(Object obj) to execute when i call, s.diaply(null), since object super class of all.
Then why this output?

thanks in advance.
yogesh
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here the concept of the most specific method comes into play....

consider the following case:

there is a value A and for this value there are two contenders...B and C

at this point the compiler has to decide as to whom the value A must be given....that is which one is more specific and which one is general

consider the following case:

class b
{
public static void main(String args[])
{
b bb=new b();
bb.display(7);
}

public void display(float obg)
{
System.out.println("in display with OBJECT argument");
}
public void display(double str)
{
System.out.println("in display with STRING augument");
}
}

in this case the concept is...a value that is assignable to float is also assignable to double...but...a value that is assignable to double can it be assinable to float...so the specific one is that of float...

so in your case string is choosen....hope it helps.

amal shah
 
yogesh gargate
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi amal,
Thanks for the reply.
Well does it means java always give preference to "MOST SPECIFIC" class?

regards,
yogesh
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, not always, but within reason.
It won't for example (unless I'm seriously mistaken) take a more specific method from a parentclass if a less specific but applicable method can be found in the class of the instance on which the method was called.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by yogeshg gargate:
Hi amal,
Thanks for the reply.
Well does it means java always give preference to "MOST SPECIFIC" class?

regards,
yogesh


When calling overloaded methods, the Java compiler always gives preference to the method with the most specific argument types, yes.

In most cases, that is actually what you want. Imagine the two methods

foo(Object o)
foo(String s)

When you call

foo("Hello World")

you probably want the method with the String parameter to be called, which is more specific than the Object parameter.

Notice that you can "override" this behaviour with casting. Both

foo((Object) "Hello World")
foo((Object) null)

would call the Object version of the method. Typically the need to do this is a strong code smell, though.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The relevant section of the Java language specification is http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.5
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!