6

I have an interface with these two methods.

E2EResult sendRoutingRequests(List<RoutingRequest> routingRequestsList);

E2EResult sendRoutingRequests(List<String> routingRequestsList);

The compiler shouts for:

java error both methods have same erasure

I saw some posts talking about same erasure for java generics.

Can someone explain why is that?

It's different q than this post, because I don't deal with wild card.

Community
  • 1
  • 1
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • FYI: It is not different than that other post. – laune Mar 03 '15 at 11:25
  • It's not different than the post you linked. It's the same issue (same signature after type erasure) dispite the wildcards. – Paco Abato Mar 03 '15 at 11:25
  • That's not the question, but what is the semantic behind both methods? If the `List` is kind of _raw_ routing requests, you can name the method explicitly, e.g. `sendRawRoutingRequests(...)` – T.Gounelle Mar 03 '15 at 13:22

1 Answers1

4

The compiler removes the generic type parameters, so List<String> and List<RoutingRequest> become List, and thus both methods have the same signature.

laune
  • 31,114
  • 3
  • 29
  • 42
Eran
  • 387,369
  • 54
  • 702
  • 768
  • but doesn't it compile to different class files? `List` and `List` – Elad Benda Mar 03 '15 at 11:24
  • 2
    @Elad Benda No it doesn't. You can easily find out: `List l = ...; List l2 = ...; System.out.println(l.getClass().equals(l2.getClass()));` – SME_Dev Mar 03 '15 at 11:26
  • Generics types are removed before compiling so the compiler doesn't see generic types. It only sees List. – Paco Abato Mar 03 '15 at 11:26
  • @EladBenda It is compiled as `List`, and RoutingRequest and String are instanceof Object. – laune Mar 03 '15 at 11:29