File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Overloading" Watch "Overloading" New topic
Author

Overloading

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
i have two methods whose name are the same,and since they are in the same class so oberloading would takes place.But if i have
methods defined as follows
class dummy{
void Firstfunc(int a,int b,int c)
{ ....................... }
float Firstfunc(int a,int b,int c)
{ ........................ }
}
since it is clear that the the diference in two methods is only of return type can anybody tell me that is it possible?
Anbooo Sanygao
Ranch Hand

Joined: Oct 04, 2000
Posts: 45
Did you try writing a small piece of code
ravi chan
Greenhorn

Joined: Nov 02, 2000
Posts: 11

class dummy{
void Firstfunc(int a,int b,int c)
{ ....................... }
float Firstfunc(int a,int b,int c)
{ ........................ }
}

it is not possible, in order to overload the method signature has to be different. So change the arguments to :
float Firstfunc(float a, int b, int c). Return type can be any data type when you are overloading. Please correct me if I am wrong. And also a thing to note is you cannot change the overloaded declaration to float Firstfunc(long a, long b, long c)
will give you a compiler error if your method call is Firstfunc(1, 2, 3) because the compiler gets confused about both the overloaded methods.

Gautam Pandya
Greenhorn

Joined: Sep 29, 2000
Posts: 17
Ravi,
Your explanation is really good but I think the last part as quoted under is not correct. The method can be overloaded with a
method float Firstfunc(long a, long b, long c).
I mean, there is nothing wrong with the given code if modified as below.

Gautam.
Originally posted by ravi chan:

. . . And also a thing to note is you cannot change the overloaded declaration to float Firstfunc(long a, long b, long c)
will give you a compiler error if your method call is Firstfunc(1, 2, 3) because the compiler gets confused about both the overloaded methods.


prasanthi kothapa
Ranch Hand

Joined: Oct 19, 2000
Posts: 30
hi ameen,
changing the return type doesn't come under
overloading, since the signature of the
method doesn't change(only the method name
and arguments come under the signature).
hence you will get a compiler error in this case.
hope it helps..
prasanthi
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hey Ravi,
you can overload the method with a signature Firstfunc(long a, long b, long c).
Originally posted by ravi chan:
And also a thing to note is you cannot change the overloaded declaration to float Firstfunc(long a, long b, long c)
will give you a compiler error if your method call is Firstfunc(1, 2, 3) because the compiler gets confused about both the overloaded methods.
[/B]

Compiler will not get confused, cause Firstfunc(1, 2, 3) can be uniquely identified. 1, 2, 3 are ints, so he will call the Firstfunc(int a, int b, int c)...
Same with floats and doubles....
hope that helps
Oliver
 
IntelliJ Java IDE
 
subject: Overloading
 
Threads others viewed
help understanding .equals.
Mock question
A question from MindQ
Reference to method is ambiguous??????
SCJP: Threads Qs
developer file tools