• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does the same return type is optional in overloading a method ?  RSS feed

 
Vishal Fichadiya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
In java when we overload a particular method the return type is made optional i.e. it may or may not be same but the method signature(parameter list) must be different. Why the same return type is made optional in the case of method overloading
i.e. one can overload method with same return type or differnect return type.
Where there is no strictness regarding return type of the overloaded method which is in the case of overridden method ?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloaded methods are different methods; the choice between overloaded methods is made at compile time, so the compiler knows what return type to expect after it makes the choice.

Overridden methods are the same method; the choice of which class's method to use is made at run time, so the compiler has no way to deal with possible differences in the return type. Hence there must not be any differences.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Fichadiya wrote:i.e. one can overload method with same return type or differnect return type.

for User(Programmer) convenience . is not it?
Vishal Fichadiya wrote:
Where there is no strictness regarding return type of the overloaded method which is in the case of overridden method ?

it is true until java1.5 . Covariant type allow you to define a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the super class. This feature removes the need for excessive type checking and casting.
 
Zach Fredrickson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the case of a method that takes a primitive type and outputs the same primitive type (i.e. public int someMethod(int i) ) that you want to overload so it does it's job no matter what primitive type is fed to it.

When changing the argument/parameter list (overloading the method) to accept a different primitive type (i.e. boolean) you can also change the return type and return the value in kind. (i.e. public boolean someMethod(boolean b) )

If only the argument list was changed but not the return type, the method may try to return an int when a boolean is taken in the argument list, which may not make sense in your method.

If overloading in this way did not allow different return types, you would have to create a method with a unique name for each primitive type you wanted your method to accept and return.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!