• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overloading

 
H. J. Yoshi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you, for the explanation and advice.I do kind of understand it now..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic