Overloading with another return type in Java?

Why is it impossible to overload a function simply by changing the return type? Will this change in a future version of Java?

By the way, just for reference, is this possible in C ++?

+74
java overloading
Mar 13 '10 at 19:49
source share
8 answers

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

public int foo() {...} public float foo() {..} ... foo(); // which one? 
+130
Mar 13 '10 at 19:51
source share

The reason is that Java overloads are allowed only for methods with different signatures.

The return type is not part of the method signature, and therefore cannot be used to distinguish between overloads.

See Defining Methods from Java Tutorials.

+41
Mar 13 '10 at 19:53
source share

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 object called the covariant return type. You can override a method with the same signature, but returns a subclass of the returned object. In other words, a method in a subclass can return an object whose type is a subclass of the type returned by a method with the same signature in the superclass.

+21
Jan 19 '13 at 16:54
source share

Overloaded methods in java can have different types of return data, given that the argument is also different.

Check out the sample code.

 public class B { 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"); } } 
+5
Oct. 15 '16 at 7:36
source share

The compiler does not consider the return type when differentiating methods, so you cannot declare two methods with the same signature, even if they have a different return type.

+1
Jul 23 '13 at 6:38
source share

The return type does not matter when the method is overloaded. We just need to make sure 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 the same types of arguments, there would be no way to determine which one it should name.

+1
Jul 18 '16 at 9:39
source share

The compiler does not consider the 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 know about the execution of the function, then you will know that when we call the function that is executed by the part of the definition, and finally we require the return statement, so we can say that the return comes after the completion of the whole function, which is why there are two or more functions with the same name and with the same type are not. arguments, and then during a call of how the compiler will know which one will be called, because the name and parameters of the function are the same. During the call, firstly, all the tricks will be focused on the arguments and the name of the function, and after completing the definition of the function, finally, we are dealing with the return statement.

A compile-time error is better than a run-time error. Thus, the java compiler displays a compiler time error if you declare the same method having the same parameters.

0
Jul 17 '17 at 7:45
source share

no it is really impossible that you can overload only any arguments or data type of arguments

-one
Feb 27 '17 at 15:08
source share



All Articles