6

Is it possible to have two methods with the same name but different parameters and return types in Java? Seems like it would be a good way to generalize a simple getter and setter.. You can do that with constructors why not with regular methods ? for example

why not be able to do ..

int getVal() {

return int;
}

boolean getVal() {

return true;

}

setVal(int a) {
}

and

setVal(boolean a) {

}
toolkit
  • 49,809
  • 17
  • 109
  • 135
Ayrad
  • 3,996
  • 8
  • 45
  • 86
  • possible duplicate of [Java - why no return type based method overloading?](http://stackoverflow.com/questions/2744511/java-why-no-return-type-based-method-overloading) – nawfal May 28 '13 at 08:08

8 Answers8

15

What would you expect if I called:

getVal();

with no return vaue being collected ? You have two choices - either the boolean or the integer variant. Since you can't enforce the return value to be collected, the compiler can't determine which variant to be called.

You can overload on method parameters, but not on the return types alone, since that's ambiguous (as shown above).

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • ideally if it's a boolean that calls it.. for example.. if (getVal == true) then it should call the getVal that returns a true.. otherwise if it's int a = getVal() it should call the one that returns an int. – Ayrad Nov 04 '09 at 10:12
  • 2
    As I've illustrated, you can't *force* a return value on this – Brian Agnew Nov 04 '09 at 10:15
11

Because then the compiler would be unable to figure out:

setVal(getVal());

should it call the bool or int version?

Yngve Hammersland
  • 1,634
  • 2
  • 14
  • 28
  • if it receives a boolean as a paramter then it should use the setVal that accepts and sets a boolean – Ayrad Nov 04 '09 at 10:05
  • like when a constructor accepts different parameters but can have the same name. – Ayrad Nov 04 '09 at 10:06
  • 4
    But how would the compiler select the bool version of getVal in the first place? It clearly has two equally valid choices. – Yngve Hammersland Nov 04 '09 at 10:07
  • by the value that type that calls it.. if it's a boolean it should choose the one that returns a boolean, if it's an int for example int a = getVal() should use the getVal that returns an int – Ayrad Nov 04 '09 at 10:13
  • As Tnay points out, the compiler only considers the method name and number and type of arguments when resolving method calls. – Yngve Hammersland Nov 04 '09 at 10:22
  • 1
    Actually, it *couldn't* have figured out the int/boolean problem if there was both a `setVal(boolean)` and a `setVal(int)`. As for the String/Object situation, it could have used the same resolution rules it does when it encounters the call `foo("bar")` and there's both a `foo(String)` and a `foo(Object)` to choose between - i.e., use the most specific one. – gustafc Nov 04 '09 at 12:33
  • 1
    I think even if it would be feasible it will pose great performance degradation since JVM will have to cope with all these testings while deciding which method to call. Anyway, I think this way it force us to be more neat and responsible in designing our systems. – ante.sabo Nov 04 '09 at 14:11
6

At first glance it may seem as if there should be no reason that one should not be allowed to do this, but however think about it from the perspective of code that must call this(these) method(s), how would it know which method to invoke?

From java.sun.com

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").

Overloaded methods are differentiated by the number and the type of the arguments passed into the method.

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.

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.

Community
  • 1
  • 1
Tom Neyland
  • 6,860
  • 2
  • 36
  • 52
5

As far as the Java Virtual Machine is concerned, it is possible for a class to declare multiple methods with the same signature but different return types.

Only, Java as a language forbids this.

eljenso
  • 16,789
  • 6
  • 57
  • 63
4

Different return types, no. But different parameter types / length, yes. That's how Java is... specification says that. They wanted to keep it simple.

Raze
  • 2,175
  • 14
  • 30
4

Generally speaking - no. But if you want it very much - then YES :)) Check this great article

St.Shadow
  • 1,840
  • 1
  • 12
  • 16
0

According to the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4.2):

It is a compile-time error to declare two methods with override-equivalent signatures (defined below) in a class.

Two methods have the same signature if they have the same name and argument types.

Oded Peer
  • 2,377
  • 18
  • 25
0

You can declare the two setters in your case - try it.

Methods must be unique in terms of their name, and the number and type of their arguments. The return type, as well as the throws clause, do not count in terms of making a method unique (which makes sense, as they're not specified when you invoke it).

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228