Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Overloading with different return type [duplicate]

Tags:

I am want to dig in that whether it is an ambiguity or an extra feature that is provided:

 public class Foo   {      public int Bar(){        //code     }      public string Bar(int a){        //code     }  } 

Any one having any experience with this, overloading on return type with different parameters should be a bad practice, is it?

But if the overloading was done on the basis of return type then why this is not working for.

 public class Foo   {      public int Bar(int a){        //code     }      public string Bar(int a){        //code     }  } 

As it will be unable to decide which function to call 1st or second, if we call obj.Bar(); , it should end in error do any one have any idea about it why it allows first code snippet to run.

like image 213
Adil Abbasi Avatar asked Dec 20 '13 14:12

Adil Abbasi


People also ask

Can we overload same method with different return type?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

Why method overloading have different return types?

The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.

Does return type matters in method overloading?

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.

Should return type be same in method overloading and overriding?

In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Return type must be same or covariant in method overriding.


2 Answers

The C# specification (section 10.6) states that overloaded members may not differ by only return type and as per http://msdn.microsoft.com/en-us/library/ms229029.aspx

As per your question regarding creating parameters simply to support differing return types? I personally believe that is a terrible solution to the problem. Code maintenance will become difficult and unused parameters are a definite code smell. Does the method really need to be overloaded in that case? Or does it belong in that class? Should something else be created to convert from one return type to another? All things you should ask to derive a more idiomatic solution.

like image 132
Jesse C. Slicer Avatar answered Oct 25 '22 18:10

Jesse C. Slicer


This is logically impossible. Consider the following call:

object o = Bar(42); 

or even

var o = Bar(42); 

How would the compiler know which method to call?

Edit: Now that I understand what you're actually asking, I think overloading by meaningless parameters is bad practice, and diminished readability, it is much preferable to distinguish by method name:

 string BarToStr()  {   }   int BarToInt()  {   } 
like image 24
Rotem Avatar answered Oct 25 '22 17:10

Rotem