Why return type of a method is not considered in method overloading ?
Can somebody explain , how compiler checks for overloaded methods ?
Why return type of a method is not considered in method overloading ? Can
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The main reason is that if you did consider this, a lot of function calls become ambiguous if the return value is not assigned to something. For example, which function call is being invoked here?
There are other reasons to not want to do this, too. That said, many languages do allow their signatures to differ only in return type; Haskell and Perl are two examples. If your language does allow this, it’s not hard to see that all you’d need to support this is one more step in your method-resolution process: simply have an obvious way for the compiler to select one method or another. In the example above, perhaps we would define a priority (maybe the first method defined is the one that wins, for example, so our
x()call would invokeString x()).Another thing to note is that the JVM does allow two methods whose signature differs only in return type to exist. (That’s how Scala and other JVM languages that support this do it.