For example can i have something like this:
public static void SAMENAME (sameparameter n)
{
some code ;
}
public static String SAMENAME (sameparameter n)
{
similar code;
return someString ;
}
No, you cannot overload a method based on different return type but same argument type and number in java.
If both methods have same parameter types, but different return type than it is not possible.
The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.
Overloading can happen in same class as well as parent-child class relationship whereas overriding happens only in an inheritance relationship.
This isn't allowed.
The method signature in Java is considered to be the method name and parameter list. The return type is not part of the method signature.
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
Source: http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html
This isn't allowed. For the compiler it is possible that several fit. For instance:
SAMENAME(n);
Could return a String or be void, both are valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With