• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Overloading

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try writing a small piece of code
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
crispy bacon. crispy tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic