File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Overloading" Watch "Method Overloading" New topic

Method Overloading

H. J. Yoshi

Joined: Jul 21, 2006
Posts: 29
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

Joined: Jul 11, 2001
Posts: 14112
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!

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
H. J. Yoshi

Joined: Jul 21, 2006
Posts: 29
Thank you, for the explanation and advice.I do kind of understand it now..
I agree. Here's the link:
subject: Method Overloading
It's not a secret anymore!