Explicit method generation and signature

I am trying to create overload methods in java:

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

But eclipse complains: The getResponse(List<ClassA>) method has the same erase getResponse(List<E>) as another method in the BaseisInformationEndpoint type.

I thought the method signature is the name of the method + list of options .... but how can List<ClassA> be the same as List<ClassB> ? Doesn't make sense to me.

+4
source share
8 answers

Java erasing a generic type will make it

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

After erasing the styles for the compiler is the same.

+5
source

Generic types ( <...> ) are present only until the compilation stage, which will be used for static input.

After compilation, these types are erased and List<ClassA> essentially becomes List . So you can see that when this happens, your two functions become identical.

This is called type erasure, as noted by the commentator.

+2
source

It's impossible. For backward compatibility, common parameters are discarded at runtime (unlike, for example, C #). This is called type erasure.

Therefore, a List<Whatever> , at run time, is simply a List . This means that both of your methods have a prototype BasesResponse getResponse(List) , which is a compilation error.

+1
source

The Java compiler also erases type parameters in the arguments of common methods.

You can see two parameters as different types, but when you delete <> the JVM will see two methods as the type of its parameters.

Therefore, overloading is not performed.

+1
source

Erase General Type:

The reason for this is because it is related to Generics, List<ClassA> and List<ClassB> . Before java 1.5 there were no Generics, List were declared as is. like a List . This means that you can put anything on the specified list before it is legal to add Object , String , ClassA , Listener , etc. Only to one list. Generalizations have been introduced to indicate the collections they will receive. Here: List<ClassA> , List<String> etc., to play.

However, in order to preserve the obsolete systems of those who created their system pre-dipide time, this will be a problem, Generics is only compilation time, but at runtime it will still be the same base list before.

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

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

+1
source

This is because after passing the erasure type, both methods will come out as follows:

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

Two identical methods with the same signature. Which one is not overloaded, but a compile-time error.

+1
source

Because for java , List<ClassA> and List<ClassB> the same as List<E> .

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

0
source

This is because the compiler still supports legacy code, and therefore it erases the generic type considering. You can find the same question answered here

0
source

Source: https://habr.com/ru/post/1487241/


All Articles