• 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

Doubt on method overloading

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets Assume I have two classes Class A and Class B, When I compile both the classes Class A i get compile time error but not for Class B why is that can some one please explain?


 
Ranch Hand
Posts: 49
Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java
1.) ambigous method are not allowed .
2.)java will call most specific method first
3)here when you call method name calcAverage(2,3) which means you are passing int arguments but as int can automatically cast to double so here it will be confusion for compiler which method to call like callAverage(int ,double ) or callAverage(double,int) this is due to implicit upcasting happens
javatpoint.png
[Thumbnail for javatpoint.png]
ambiguity refer javatpoint
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Revathy Balajee wrote:Lets Assume I have two classes Class A and Class B, When I compile both the classes Class A i get compile time error but not for Class B why is that can some one please explain?



When the Java compiler needs to disambiguate between two possible methods, it uses the "most specific method" rule. And in the case of class A, the rule fails to work, and hence, you get a compile error.

For more details, please search for "most specific method" on the ranch forums. Thanks.

Henry
 
Revathy Balajee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Class B still compiles why the ambiguity does not happen there because an int can be upcasted to long right?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Revathy Balajee wrote:But Class B still compiles why the ambiguity does not happen there because an int can be upcasted to long right?



The "most specific method" rule works in this case. An int can be implicitly cast to an long, but a long can't be implicitly cast to an int, hence, the method that takes the int is most specific.

Henry

 
Revathy Balajee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not able to find the "most specific method" rule. If you don't mind can you please provide a link to the same?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Revathy Balajee wrote:I'm not able to find the "most specific method" rule. If you don't mind can you please provide a link to the same?



It is defined in section 15.12.2.5 of the Java Language Specification...

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

Henry
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic