File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Doubt" Watch "Inheritance Doubt" New topic

Inheritance Doubt

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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 ....
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

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.

Jeff Hu

Joined: Feb 03, 2005
Posts: 16
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.

SCJP 1.4, SCJA 1.0
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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

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.

SCJA 1.0 (98%), SCJP 1.4 (98%)
I agree. Here's the link:
subject: Inheritance Doubt
It's not a secret anymore!