aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling overloaded methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling overloaded methods" Watch "Calling overloaded methods" New topic
Author

Calling overloaded methods

Samy Venkat
Greenhorn

Joined: May 26, 2003
Posts: 10
class Overload001{
int print(int a,long b, long c){
return (int)(a+b+c);
}
int print(long a,int b, long c){
return (int)(a+b+c);
}
public static void main(String sam[]){
Overload001 ol1=new Overload001();
System.out.println(ol1.print(1,2,3));
}
}
The above code doesn't compile because the compiler cannot determine which method to call. How method calls are resolved. Is it based on the first parameter or method with all parameter matching. But below code is working. Pls. explain.
public class Overload002{
private void locate(double a, long b){
System.out.println(a*b);
}
private void locate(float a, int b){
System.out.println(a*b);
}
public static void main(String arg[]){
new Overload002().locate(1.0,2);
}
}
In the above program, based on the first parameter the with double should be called. But based on the second parameter, second locate method should be called. Pls. explain. Thanks.
Laurency Heo
Greenhorn

Joined: May 21, 2003
Posts: 9
overide method are distingushied on the method name and parameter signature.
a new method need define as
int print(int a,int b, int c)
---------------------------
class Overload001{
int print(int a,int b, int c){
return (int)(a+b+c);
}
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
in first case, the arguments 1, 2 and 3 can be implicitly int or long....so both methods are likely to be called.
but in the second case, 1.2 is a double, so .....only the first method could be called.
[ June 03, 2003: Message edited by: Yi Meng ]

Meng Yi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Samy
The problem is that both the methods(print()) match the supllied agruments. So the compiler is totally confused as to which method should be invoked. You can help your complier by telling him exactly which method needs to be called by casting the value into long(in this case). eg.

You can consider this as simply calling a person with a name. But what if the name belongs to more than one person. Now here you need to be a bit specific. Same is the case with java.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Samy,
The first piece of code failed because the compiler could not determine the method that is most specific to the call. The argument supplied in the call are all integral literals, and by default they are set to int, and int can be promoted to long. So both methods satisfy the call.
As for the 2nd piece of code, one of the argument supplied is a floating-point literal, and by default it is set to double. Now only one method satisfy this (double could not be demoted to float or lower), so no compiler error here.
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
In the following code, if null is passed as a parameter to method print, the compiler complains because both the method signatures match. However, if any string is passed, it compiles and prints "String: ABC "
On what basis does the compiler chose which method is most appropriate?

Thanks,
Saket
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Originally posted by Brian Joseph:
[QB]>On what basis does the compiler chose which method is most appropriate?

This is an easier question to answer because promotions are not involved. If you pass a null literal (not a null reference), and the method is overloaded, the compiler has no idea what method to pick.
However, you learn early on that any string literals are translated into String objects at compile time, so therefore that "Saket here" is translated into a String object and passed to the method print( String s).
Try doing this:

We are using references. Their value is null, but there is a difference. The compile knows the type of reference being passed and has no problem picking the right method to call.
The key thing here is the difference between passing a null reference and a null literal.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Also, in regards to Samy's original post, I think I understand the basics of why the call is ambiguous in the case of the literal numbers.
Rule 1: An integer literal is of type int. But if the literal is being assigned or passed to a variable of type long, it is implictly promoted by the compiler - This is why the first case was ambiguous.
Rule 2: All floating point number literals, such as 1.0, are treated as double. You would think that it would be similiar to int and be of type float, but that is not the case. Amazingly, float myFloat = 1.0; gives a compile error! PROOF!

But what about that JLS about calling the more "specific" method. What is considered more "specific"? I really don't get that statement. I thought in the case of object references, the compile shoots for the subclass all the time, is that true?
[ June 03, 2003: Message edited by: Brian Joseph ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Brian
For what more specific means consider the following code :

Output :float
When you invoke the methods with an int input which method should be invoked. As float is more specific so float one would be invoked.
Also keep in mind that you may get code like the one in this post. In my code actually the methods are overloadded but it might appear because of the method name that the methods are overridden.
[ June 03, 2003: Message edited by: Anupam Sinha ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
It looks like the same promotions rules apply for passing literals to overloaded methods. Although there is no implict down-casting. I tried to pass a 1 to overloaded methods that take a byte and a short, and it doesn't work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling overloaded methods
 
Similar Threads
javaprogramming
Passing primitive values to methods and method overloading
Question on Overriding
Query
Question regarding covariant returns