Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overloading For int and long ????

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I couldn�t find the different between example 1 and 2.
But the example 1 complies and run�s fine but the example 2 gives compilation error. I would like to know what is happening???

Thanks, Raghu.K

Error message of Example 2:-
C:\java>javac Test.java
Test.java:14: reference to getVal is ambiguous, both method getVal(int,int) in Base and method getVal(long,long) in Derived match
System.out.println(d.getVal(10, 7));


 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No compilation error for me. Output "3".
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your Example 2, and I got output of 3 without any errors/exceptions.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have a literal number in your source code, it's an int. You can see below how to have a literal long.


Here's the kicker: while polymorphism (overriding methods) is done during runtime, overloading is done at compile time. Therefore, whatever your variable is typed as will determine which method is run. Since your literal is an int, it calls the int-overloaded method. You can cast it as a long if you'd like to get the long-overloaded method.

This is best illustrated when you have a null value, but want to invoke a specifically-overloaded method. (Which isn't really the best design-wise, but oh well.)


You should get a compile-time error saying the call to invoke is ambiguous. There's no type associated with a null value, so it doesn't know which method to call (a static, compile-time problem).

Fix this by casting the null value to either String or Date.
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Wise and Sundar,

Which version you guy's are trying, I am using 1.4 and These are the errorme message i got when i compile

Thanks, Raghu.K

 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with Java 1.5 from eclipse IDE, and with 1.4 from a command line.
Both worked fine with output of 3.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with Java 1.5 and the program compiles and gives an output of 3
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic