Sobes.tech
Junior — Middle

Is it possible to change the return type of a method when overloading it with different parameter sets?

sobes.tech AI

Answer from AI

In Java, method overloading allows having methods with the same name but different parameter sets. However, changing only the return type without altering parameters is not possible, as the compiler cannot unambiguously determine which method to call.

Example:

// Correct:
void print(int a) {}
void print(String s) {}

// Incorrect:
int getValue() {}
String getValue() {} // Compilation error: duplicate method

Thus, the return type is not considered in overloading, and for successful overloading, methods must differ by their parameters.

Is it possible to change the return type of a method… - sobes.tech