• 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

Inheritance Doubt

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi for the below code i got the compiler error saying ambigous method...





But, when i interchange the both method in square and rectangle still i got the compiler error saying loss of precision....

why in both case it throws compiler error

please clear my doubt ....
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the method signature of area() is different, Square.area() does not override Rectangle.area(), but Square still inherits the area method of Rectangle. Since an int can be promoted to a long, the compiler cannot resolve which method to call.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:



The underlying problem is area method in Rectangle class. Method return type is long, while it expects int. Loss of precision here. That's why as long as you didn't make proper change, it will keep giving you compilation error. You should change its arguments to int, or explicit cast return value to int, or make method area expect type of long value.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compiled the code you posted, I did not get a compiler error saying "ambigious method". I got a compiler error saying "possible loss of precision" because length * width evaluates to a long, which does not fit into the return value of int.



Parameswaran, I did as you suggested and swapped the methods in Square and Rectangle, and I still only got a "possible loss of precision" error:



In both cases the reason is that same: the return value is a long but the method is declared to have a return value of int.

If we change the first example so that the return value is cast to an int, we get this:

This compiles and runs correctly, with an output of
25

I disagree with Edwin that the compiler does not know which method to call. Given this situation, the compiler will always attempt to choose the method with the most specific parameter datatypes. In this case, all the values that can fit in an int can also fit in a long, but other values can also fit in a long that cannot fit in an int. That makes int more specific than long. The compiler will choose the method with the most specific datatypes, if and only if the parameter datatypes of one method are clearly more specific than the parameter datatypes of the other method.

Parameswaran, if you compile your posted code again I think you'll find that it does not report an error of "ambiguous method" as you said. Perhaps you got that error from a different version of the program that you did not post. There is no ambiguous method call in this program as it is written.
 
reply
    Bookmark Topic Watch Topic
  • New Topic