This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Welcome to the Ranch
Please don’t post new messages on old threads; I have split this discussion from this old thread.
Please review the Java Language Specification section about choosing overloaded methods. This example shows how confusing and hazardous overloading can be, and how careful you have to be about it. You think you are choosing between boxing an argument 5 to an Integer versus widening it to a long, but that is not actually what you have written.
The parameters for those methods are of type long and Integer. You can widen an int to a long, but not to a long. You can box it to an Integer, but not to an Integer.
If you try boxing, you can possibly match one method, and if you try widening, you can match the other.
So you can only match those methods with boxing AND, or widening AND. So they are not more specific than each other. So I tried it and got this compiler error:
BMW.java:16: error: reference to getModelno is ambiguous, both method getModelno(long...) in BMW and method getModelno(Integer...) in BMW match
Thanks Campbell Ritchie..
I understood that Java Language Specification section.
Specially this line in Java Language Specification section:
A method m1 is strictly more specific than another method m2 if and only if m1 is more specific than m2 and m2 is not more specific than m1.
In my code, each method is not more specific than the other one. I have to make a method more specific to make my code work.
Joined: Oct 13, 2005
Remember what I said yesterday:
This example shows how confusing and hazardous overloading can be, and how careful you have to be about it.