'Overload with different return type in Java?
Why is it not possible to overload a function just by changing the return type? Will that change in a future version of Java?
By the way, just for reference, is this possible in C++?
Solution 1:[1]
The reason is that overloads in Java are only allowed for methods with different signatures.
The return type is not part of the method signature, hence cannot be used to distinguish overloads.
See Defining Methods from the Java tutorials.
Solution 2:[2]
Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.
Solution 3:[3]
Overloaded methods in java may have different return types given that the argument is also different.
Check out the sample code.
public class B {
// Method to be overloaded
public String greet() {
return "Hello";
}
//This will work
public StringBuilder greet(String name) {
return new StringBuilder("Hello " + name);
}
//This will not work
//Error: Duplicate method greet() in type B
public StringBuilder greet() {
return new StringBuilder("Hello Tarzan");
}
}
Solution 4:[4]
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Solution 5:[5]
Return type does not matter while overloading a method. We just need to ensure there is no ambiguity!
The only way Java can know which method to call is by differentiating the types of the argument list. If the compiler allowed two methods with the same name and same argument types, there would be no way to determine which one it should call.
Solution 6:[6]
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
If you are aware of function execution then you will be aware that when we call a function the definition part executes and at last we require the return statement, hence we can say return comes after the function's whole defintion, thats why if there are two or more functions with same name and with same type and no. of arguments then at the time of calling how compiler will know about which one to be called, because function name and parameters are the same. At the time of calling firstly all the focus will be on arguments and function name and after the completion of function definition at last we deal with return statement.
Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.
Solution 7:[7]
The overloaded method is a completely different method from any other method of the same name. Overloading is not much more than name reuse.
Solution 8:[8]
public int division(int a, int b){
int result = a/b;
return result;
}
public double division (float a, float b){
double result = a/b;
return result;
}
i am pretty sure that in java 8
it is ok
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oded |
| Solution 2 | Daniil Iaitskov |
| Solution 3 | |
| Solution 4 | Ganesh Kumar Palaniappan |
| Solution 5 | Evan Porter |
| Solution 6 | Bhanu |
| Solution 7 | Java |
| Solution 8 | 陳寧寬 |
