Why does return type of method not included in signature?
An example
public void method1(String arg){...}
public String method1(String arg){...}
It will cause an error.
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.
This is done because the compiler would not be able to figure out the overload in all contexts.
For example, if you call
the compiler knows that you are looking for the second overload. However, if you call
like this, the compiler has no idea which one of the two methods you wanted to invoke, because it is OK to call a method returning
Stringand discard the result. To avoid ambiguities like this, Java prohibits overloads that differ solely on the return type.