Reputation: 4342
In method overloading, is it possible to have different return types for a overloaded method? for example,
void foo(int x) ;
int foo(int x,int y);
double foo(String str);
in general object oriented programming, is it possible?
Upvotes: 46
Views: 126814
Reputation: 2629
Answer is NO.
Integer doSomething(){return null;}
String doSomething(){return null;}
main(){
doSomething()
}
In this case compiler will never know which method to be invoked.
Note - Method Overloading is checked during compile time
Upvotes: 5
Reputation: 1348
Yes, it is possible if the parameters are also different in the overloaded method.
But if you have two methods with the same parameters and with a different return type, it'll produce a compiler error. I repeat: Different parameters are mandatory for overloaded methods irregardless of their return type.
Upvotes: 2
Reputation: 280
In short, overloading the return type needs a powerful typing system which can derive the function to be used from the calling result.
Haskell and ML family can accomplish this job.
It's not right to define overloading to be restricted in changing the args since changing the return type leaves enough information for the compiler to derive which function to be used.
Upvotes: 4
Reputation: 495
It is not possible to have a method with same parameters and different return
type.
Compiler throws error
in the below case(Duplicate method).
Method 1:
public int calc(int a, int b, int c)
Method 2:
public String calc(int e, int f, int g)
Error thrown:
method calc(int, int, int) is already defined
Upvotes: 24
Reputation: 39
You can use the Varadic template feature of C++ Hear is the Example:
template<typename T>
T adder(T v) {
return v;
}
template<typename T, typename... Args>
T adder(T first, Args... args) {
return first + adder(args...);
}
And here are a couple of ways we could call it:
long sum = adder(1, 2, 3, 8, 7);
std::string s1 = "x", s2 = "aa", s3 = "bb", s4 = "yy";
std::string ssum = adder(s1, s2, s3, s4);
For the more refrence https://eli.thegreenplace.net/2014/variadic-templates-in-c/
Upvotes: 3
Reputation: 103
This is not valid for overloading. You can only overload by changing the parameter list. Changing the return type alone is not valid for method overloading as the return type is not part of the method signature (just the method name and parameter list are in the method signature).
Upvotes: 3
Reputation: 1076
Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.
Important Points
Upvotes: 31
Reputation: 191
yes the example given by you is Possible we can do method ovrloading in this way in c#
void foo(int x)
{
}
int foo(int x, int y)
{
return 0;
}
double foo(String str)
{
return 1;
}
Upvotes: 3
Reputation: 199
No method overloading is not possible in case of different return type, because compiler can't figure that which method he need to call..
Upvotes: 5
Reputation: 1408
Check out this awesome answer, Function overloading by return type?
In short, most statically typed languages don't, but some dynamically typed languages can.
Edit: The "In short" answer applies to overloading strictly by return type. As others have pointed out, if the parameter lists differ, and can be resolved by the compiler, then each method may return a different type. It is possible to overload methods only by return type in ADA, since the return value cannot be ignored, and the compiler can resolve the method call using this information.
Upvotes: 3
Reputation: 4342
In a class, there can be several methods sharing the same name but differ in
By depending on the parameters provided for the method, in the run time, compiler determines which version of the method to execute.
An overloaded method may or may not have different return types. But return type alone is not sufficient for the compiler to determine which method is to be executed at run time.
Upvotes: 63
Reputation: 1966
As long as you don't do something like this:
int foo (int i, int has_default=0);
double foo (long l);
/* Skipping to the function call. */
foo (1);
you should be okay. The above code will cause problems because it could be trying to call either function. It can get really bad if you're using C++, and you return pointers instead of primitives or references...
Upvotes: 4
Reputation: 33956
For most programming languages that support method overloading (Java, C#, C++, ...), if the parameter types are different, then the return types can also be different.
Upvotes: 2