• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Method Overloading

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can not achieve method overloading by declaring methods differing only in the return types, instead there has to be a difference in the number and/or types of arguments to the methods.Why is it so.. I can't understand.. Please help
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should the compiler determine which method to use?

Keep in mind that it's legal to ignore the return value when you call a method!
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many cases, "omitted" features perhaps could have been included in the language (maybe with some knock-on effects on other features), but the language designers chose not to do that. They were trying to design a fast, portable, compact, easy to understand, easy to compile language, and such a language cannot have every conceivable feature.

Regarding return types, as Ilja said, allowing methods that differ only in their return type has some fairly fundamental problems, if the rest of Java stays as it is.

More interestingly, Java originally did not allow overrides that had return values that were subclasses of the return type of the superclass method. But from Java 5, it does. It's called "covariant return types". My application is stuck in Java 1.4, but hopefully goes to Java 6 soon. I'm looking forward to using covariant return types then.

General point: If you are indeed a beginner, I would advise not asking too many "why is it so?" questions at this stage. Concentrate on learning what you can do in the language, rather than getting upset about what you can't.
 
H. J. Yoshi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, for the explanation and advice.I do kind of understand it now..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic