What will be the output of the following code snippet: public int add(int a, long b) { return a + b; } and public long add(int a, int b) { return a + b; }?

  • Compilation Error: Ambiguous method call.
  • Compilation Error: Duplicate method add with the same parameter types.
  • Compilation Error: Mismatched return types.
  • The code will run without errors, and the output will be the sum of a and b.
The code will result in a compilation error because both methods have the same name and the same parameter types (int and long). Java does not allow you to overload methods based solely on the return type. Overloaded methods must have different parameter lists. Overloading based on return types would lead to ambiguity.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *