• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading using List of Generic Types as Argument

 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to do this:

Superclass : A
Subclass: AA
Subclass: AB

create 2 overloaded methods

void someMethod(List<AA> ;
void someMethod(List<AB> ;

It seems that the compiler is treating List<AA> and List<AB> as both lists so they are considered duplicate methods.

Anybody has an idea how to get around this?
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that the compiler is treating List<AA> and List<AB> as both lists so they are considered duplicate methods.


Unfortunately, they are duplicates. Generics are handled completely at compile time. The type information is not available at runtime, so both methods have the same signature.


Anybody has an idea how to get around this?


The best way is to probably create a single method that can handle all cases -- using a generic type.

If this is not possible, meaning both cases are very different, then you probably have to use an if/then with the instanceof operator, to handle both cases in a single method.

Henry
[ November 24, 2007: Message edited by: Henry Wong ]
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way to cheat around this.

Both methods are the same because after the generics are removed, both are the same: void someMethod(List).

If you give them different return types however, it will work:

After the generics are removed (this process is called erasure), the two are different. Of course it seems strange because after the erasure you have two methods with the same parameters and different return types, which is illegal when writing the code, but at this point in time the compiler already knows the difference between the two.
[ November 24, 2007: Message edited by: Rob Prime ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would probably avoid using that last workaround - at least, unless there's some reason why having different return types for the methods makes sense in the first place. Which seems unlikely in general - you indicated the methods should be void, and adding a return type just for overloading seems very counterintuitive to me. If you can't write a single method that handles both cases (possibly by being overridden in AA and AB, rather than overloaded), then I would simply give the methods different names. There's really no need for them to have the same name if they do different things.
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is not possible, meaning both cases are very different, then you probably have to use an if/then with the instanceof operator, to handle both cases in a single method.


Isn't this operation illegal?

void someMethod(List<? extends A> aList) {

if(aList instanceof List<AA>
//some code

if(aList instanceof List<AB>
//some code

}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - but you can use instanceof on the things within the List, to see if each is an AA or AB, etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic