Overload with different return type in Java?

JavaOverloading

Java Problem Overview


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++?

Java Solutions


Solution 1 - Java

You can't do it in Java, and you can't do it in C++. The rationale is that the return value alone is not sufficient for the compiler to figure out which function to call:

public int foo() {...}
public float foo() {..}

...
foo(); // which one?

Solution 2 - Java

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 3 - Java

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 4 - Java

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 5 - Java

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 6 - Java

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 - Java

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 8 - Java

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 9 - Java

no not really possible that way you can only overload by no of arguments or data type of the arguments

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnunosView Question on Stackoverflow
Solution 1 - JavaAlexander GesslerView Answer on Stackoverflow
Solution 2 - JavaOdedView Answer on Stackoverflow
Solution 3 - JavaDaniil IaitskovView Answer on Stackoverflow
Solution 4 - JavaAbdullah KhanView Answer on Stackoverflow
Solution 5 - JavaGanesh Kumar PalaniappanView Answer on Stackoverflow
Solution 6 - JavaBhanuView Answer on Stackoverflow
Solution 7 - JavaVinayakView Answer on Stackoverflow
Solution 8 - JavaJavaView Answer on Stackoverflow
Solution 9 - JavaMAYANK AMRITView Answer on Stackoverflow