• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on method overloading

 
Revathy Balajee
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?


 
yogesh doshi
Ranch Hand
Posts: 33
Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic