4

Im trying to create overloading methods in java:

private BasesResponse getResponse(List<ClassA> classA) {
...
}

private BasesResponse getResponse(List<ClassB> classB) {
    ...
}

But eclipse is complaining about: Method getResponse(List<ClassA>) has the same erasure getResponse(List<E>) as another method in type BasisInformationEndpoint.

I thought method signature is method name + parmeter list.... but how can List<ClassA> be the same as List<ClassB>? Doesnt make sense to me.

3
  • 3
    Read about type erasure: angelikalanger.com/GenericsFAQ/FAQSections/… Commented Jun 20, 2013 at 9:01
  • it does make sense, and this is a good question. i'm sure someone will type up a good explanation for you. Commented Jun 20, 2013 at 9:03
  • Once you understand Type Erasure you will understand why it is Commented Jun 20, 2013 at 9:04

8 Answers 8

5

Java generic type erasure will make it

private BasesResponse getResponse(List classA) {
...
}

private BasesResponse getResponse(List classB) {
    ...
}

After type erasure it is the same for compiler.

Sign up to request clarification or add additional context in comments.

Comments

2

The generic types (<...>) are present only before compilation stage, to be used for static typing.

Once compiled, those types are "erased" and List<ClassA> essentially becomes List. Thus you can see that when this happens, your two functions become identical.

This is called type erasure, as has been mentioned by the commenter.

Comments

2

Generic type erasure:

The reason for this is actually due to Generics, List<ClassA> and List<ClassB> Before java 1.5 there weren't any Generics, List were declared as is. just as List. Which means that you can put anything in specified list, before it would be legal to add Object, String, ClassA, Listener, etc to one List only. Generics were introduced for specifying the collections which type they would be getting. This is where: List<ClassA> ,List<String>, etc comes in to play.

However in order preserve the legacy systems of those who created their system pre-generics time this would be an issue, Generics are only imposed compile time, but in runtime it will still be the same underlying List before.

So to answer your question, to Eclipse this is both the same method signature receiving one parameter:

private BasesResponse getResponse(List classX) { ... }

Comments

1

This is not possible. For backwards compatibility, generic parameters are discarded at runtime (unlike in, for instance, C#). This is called type erasure.

Therefore a List<Whatever>, at runtime, is just a List. Which means both of your methods have a prototype of BasesResponse getResponse(List), which is a compile error.

Comments

1

The Java compiler also erases type parameters in generic method arguments.

You might see the two parameters as different type, but when the <> is removed the JVM will see two methods as the type of their parameters.

So overloading fails.

Comments

1

This is because after going through type erasure both methods will come out to be like this:

private BasesResponse getResponse(List classA) {
...
}

private BasesResponse getResponse(List classB) {
    ...
}

Two same methods with same signature. Which ofcourse is not overloading , but a compile time error.

Comments

0

Because for java the List<ClassA> and the List<ClassB> type parameter is the same as the List<E>.

You can resolve this problem only using different method names or, if ClassA and ClassB have the same parent, use getResponse(List<? extends ClassABParent> param).

Comments

0

This is because compiler still supports legacy code and that is why it erase the generic type considering.you can find the same question is answered in here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.