• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

overloaded methods

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why cant we overload methods by just changing the return type?

regards
Percy
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,

How do you differentiate between those two overloaded calls ?
 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nobody need answer the stupid question above - i think i got the answer.

the compiler wont be able to figure out which of the two methods to call coz the decision it makes is dependent on the parameter list - right?

regards
percy
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,


why cant we overload methods by just changing the return type?



Overloaded methods are selected at the compile time using the reference variable unlike the overridden methods which are selected at run time known as Run Time Polymorphism, on behalf of what object the reference variable refering to.

If you just change only the return type of the overloaded method, compiler has no way to find which method to choose. There is one and only method selection criteria in the case of overloading and that is the number of arguments and there type and their order.



No matter what the return type is, but the argument criteria(said above) must be followed.

Thanks and Regards,
cmbhatt
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,

No Problem Percy.
To Error is Human. It happens with everybody.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,

You did what should have been done in certain situation. We forget the subtle looking issues among the fascination of great "said to be" questions.


Thanks for pointing out the issue


With Regards,
cmbhatt
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic