• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic