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 ;
}
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 ;
}
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.
It's not possible to overload a method on the return type. Have a read of the "Overloading Methods" section on the Java Tutorial.
As it states,
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Here's the simplest illustration why you can't. Imagine you implement:
String overloadedMethod();
int overloadedMethod();
and now I call
overloadedMethod();
so, which one gets called ? Since the return type isn't mandated, you can't determine which method to call.
no u cant do that. coz somtimes in java, methods are called ignoring the return value which is known as "method calling for its side-effects."
consider this:
void x(){} int x(){}
x();//method call --allowed in java
how can java determine which x() is called. so overloading based on return types is not allowed in java.
No, because when the program came across the function call SameName(param n) it would not know which to use.