Method has the same erasure as another method in type
Asked Answered
D

8

435

Why is it not legal to have the following two methods in the same class?

class Test{
   void add(Set<Integer> ii){}
   void add(Set<String> ss){}
}

I get the compilation error

Method add(Set) has the same erasure add(Set) as another method in type Test.

while I can work around it, I was wondering why javac doesn't like this.

I can see that in many cases, the logic of those two methods would be very similar and could be replaced by a single

public void add(Set<?> set){}

method, but this is not always the case.

This is extra annoying if you want to have two constructors that takes those arguments because then you can't just change the name of one of the constructors.

Dryden answered 4/1, 2010 at 9:58 Comment(6)
you can differ by implementation like one is Set other take HashSet etcVoletta
what if you run out of data structures and you still need more versions?Dryden
You could make custom classes that inherit from base versions.Windermere
OP, did you come up with some solution to the constructor problem? I need to accept two kinds of List and I don't know how to handle it.Ainslee
When working with Java, I really miss C#...Exchange
@TomášZato, I solved that by adding dummy params to the constructor: Boolean noopSignatureOverload.Puerperal
Q
397

This rule is intended to avoid conflicts in legacy code that still uses raw types.

Here's an illustration of why this was not allowed, drawn from the JLS. Suppose, before generics were introduced to Java, I wrote some code like this:

class CollectionConverter {
  List toList(Collection c) {...}
}

You extend my class, like this:

class Overrider extends CollectionConverter{
  List toList(Collection c) {...}
}

After the introduction of generics, I decided to update my library.

class CollectionConverter {
  <T> List<T> toList(Collection<T> c) {...}
}

You aren't ready to make any updates, so you leave your Overrider class alone. In order to correctly override the toList() method, the language designers decided that a raw type was "override-equivalent" to any generified type. This means that although your method signature is no longer formally equal to my superclass' signature, your method still overrides.

Now, time passes and you decide you are ready to update your class. But you screw up a little, and instead of editing the existing, raw toList() method, you add a new method like this:

class Overrider extends CollectionConverter {
  @Override
  List toList(Collection c) {...}
  @Override
  <T> List<T> toList(Collection<T> c) {...}
}

Because of the override equivalence of raw types, both methods are in a valid form to override the toList(Collection<T>) method. But of course, the compiler needs to resolve a single method. To eliminate this ambiguity, classes are not allowed to have multiple methods that are override-equivalent—that is, multiple methods with the same parameter types after erasure.

The key is that this is a language rule designed to maintain compatibility with old code using raw types. It is not a limitation required by the erasure of type parameters; because method resolution occurs at compile-time, adding generic types to the method identifier would have been sufficient.

Quartile answered 11/12, 2011 at 21:53 Comment(9)
Great answer and example! I am not sure, however, if I fully understand your last sentence ("Because method resolution occurs at compile-time, before erasure, type reification is not required to make this work."). Could you elaborate a bit?Duhon
Makes sense. I just spent some time thinking about type reification in template methods, but yeah: the compiler makes sure the right method gets selected before type erasure. Beautiful. If it weren't tainted by the legacy code compatibility issues.Duhon
A couple years have passed since the last comment or edit here. Has anything changed, perhaps with Java 8? Tell me that there is a command-line option to javac which allows it to forsake old classes. That would make my day!Board
@Board No, I am not aware of such an option for javac.Quartile
Not the first time I encounter Java error which is no error at all and could be compiled if only the authors of Java used warnings as everybody else does. Only they think they know everything better.Ainslee
By the way is there any workaround to the constructor problem?Ainslee
@TomášZato which constructor problem?Quartile
@Quartile When your class has two constructors which accept two different lists, such as List<String> and List<Integer>. They operate on the lists differently but they can not exist separately due to this error. So I need a way to define both operations within one constructor that takes just List. I have no idea how to check for the correct generic types and cast given List to List<Something> as needed.Ainslee
@TomášZato No, I don't know of any clean workarounds for that. If you want something dirty, you could pass the List<?> and some enum that you define to indicate type. The type-specific logic could actually be in an method on the enum. Alternatively, you might want to create two different classes, rather than one class with two different constructors. Common logic would be in a superclass or a helper object to which both types delegate.Quartile
B
136

Java generics uses type erasure. The bit in the angle brackets (<Integer> and <String>) gets removed, so you'd end up with two methods that have an identical signature (the add(Set) you see in the error). That's not allowed because the runtime wouldn't know which to use for each case.

If Java ever gets reified generics, then you could do this, but that's probably unlikely now.

Baking answered 4/1, 2010 at 10:2 Comment(8)
I am sorry but your answer (and the other answers) do not explain why there is an error here. Overload resolution is done at compile time and the compiler surely has the type information needed to decide which method to link by address or by whatever the method is referenced in bytecode which I believe is not signature. I even think some compilers will allow this to compile.Tumbrel
@Tumbrel what's to stop the method being called or inspected via reflection? The list of methods returned by Class.getMethods() would have two identical methods, which wouldn't make sense.Lowland
The reflection information can/should contain the metadata needed to work with generics. If not how does the Java compiler know about generic methods when you import already compiled library?Tumbrel
Good question. I had to look it up. See tutorials.jenkov.com/java-reflection/generics.html Note that getMethod just takes the class of the generic e.g. Set. So getMethod would want to two return methods for the submitters code, which it can't.Lowland
Then the getMethod method needs fixing. For example introduce an overload wich specifies generic overload and make the original method return non-generic version only, not returning any method anotated as generic. Of course this should have been done in version 1.5. If they do it now they will break backward compatibility of the method. I stand by my statement that type erasure does not dictate this behaviour. It is the implementation that did not get enough work probably due to limited resources.Tumbrel
For the most part I like Java generics, and this is the correct answer, of course, but I have to say this limitation really sucks.Geum
This is not a precise answer, but it does quickly sum up the issue in a useful fiction: the method signatures are too similar, the compiler might not tell the difference, and then you'll get "unresolved compilation problems."Skiles
Actually the javac compiler from Java 1.6 did compile such methods (with the same erasure) correctly, as long as they had different return types - see #5527735. Unfortunately they "fixed" it in Java 7... :(Cyclopropane
Z
51

This is because Java Generics are implemented with Type Erasure.

Your methods would be translated, at compile time, to something like:

Method resolution occurs at compile time and doesn't consider type parameters. (see erickson's answer)

void add(Set ii);
void add(Set ss);

Both methods have the same signature without the type parameters, hence the error.

Zest answered 4/1, 2010 at 10:4 Comment(0)
S
28

The problem is that Set<Integer> and Set<String> are actually treated as a Set from the JVM. Selecting a type for the Set (String or Integer in your case) is only syntactic sugar used by the compiler. The JVM can't distinguish between Set<String> and Set<Integer>.

Sethsethi answered 4/1, 2010 at 10:5 Comment(3)
It's true that the JVM runtime has no information to distinguish each Set, but since the method resolution happens at compile-time, when the necessary information is available, this isn't relevant. The problem is that allowing these overloads would conflict with the allowance for raw types, so they were made illegal in the Java syntax.Quartile
@Quartile Even when the compiler knows what method to call, it can't as in the bytecode, they both look exactly the same. You'd need to change how a method call gets specified, as (Ljava/util/Collection;)Ljava/util/List; doesn't work. You could use (Ljava/util/Collection<String>;)Ljava/util/List<String>;, but that's an incompatible change and you'd run into unsolvable problems in places where all you have is an erased type. You'd probably have to drop erasure completely, but that's pretty complicated.Ivelisseivens
@Ivelisseivens Yes, I agree that you'd have to change the method specifier. I'm trying to get at some of the unsolvable problems that led them to give up on that attempt.Quartile
C
8

Define a single Method without type like void add(Set ii){}

You can mention the type while calling the method based on your choice. It will work for any type of set.

Congratulate answered 16/6, 2016 at 7:54 Comment(0)
P
3

It could be possible that the compiler translates Set(Integer) to Set(Object) in java byte code. If this is the case, Set(Integer) would be used only at compile phase for syntax checking.

Preexist answered 4/1, 2010 at 10:2 Comment(1)
It's technically just the raw type Set. Generics don't exist in byte code, they're syntactic sugar for casting and provide compile-time type safety.Depravity
F
3

I bumped into this when tried to write something like: Continuable<T> callAsync(Callable<T> code) {....} and Continuable<Continuable<T>> callAsync(Callable<Continuable<T>> veryAsyncCode) {...} They become for compiler the 2 definitions of Continuable<> callAsync(Callable<> veryAsyncCode) {...}

The type erasure literally means erasing of type arguments information from generics. This is VERY annoying, but this is a limitation that will be with Java for while. For constructors case not much can be done, 2 new subclasses specialized with different parameters in constructor for example. Or use initialization methods instead... (virtual constructors?) with different names...

for similar operation methods renaming would help, like

class Test{
   void addIntegers(Set<Integer> ii){}
   void addStrings(Set<String> ss){}
}

Or with some more descriptive names, self-documenting for oyu cases, like addNames and addIndexes or such.

Fluecure answered 30/11, 2019 at 13:36 Comment(0)
S
0

In this case can use this structure:

class Test{
   void add(Integer ... ii){}
   void add(String ... ss){}
}

and inside methods can create target collections

void add(Integer ... values){
   this.values = Arrays.asList(values);
}
Superfamily answered 27/1, 2023 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.