• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basics of overloading  RSS feed

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java programming. I am not able to understand the following diffrences. Any help will be much appreciAted.
CODE 1:

The this will give compiler error for obvious reason. Now,
CODE 2:

This doesn't give any copiler error. And gives the output as "in print1"
Some one please explain this..???

EDIT by mw: Added Code Tags.
[ December 31, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question about choosing the "most specific" method.

According to JLS - 15.12.2.2, "one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error."

Let's consider your second example first, because this is one that works. Here, the methods are overloaded with (int, long, int) and (long, long, int). In this case, the first method can be identified as more specific, because each of its argument types can be converted (upcast or widened) to the corresponding argument type in the second method. In particular...

Now let's consider why your first example does not work. Here, the methods are overloaded with (int, long, int) and (long, int, long). This is ambiguous because neither set of argument types can be converted to the other. In particular, the first method is NOT more specific because its second argument is a long, which cannot be converted to an int...

Also, the second method is NOT more specifc because its first and third arguments are both longs, which cannot be converted to ints...

Because this is ambiguous, a compiler error results.
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. This cleared my doubt...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!