• 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

Basics of overloading

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Aarav Thomas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. This cleared my doubt...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic