aspose file tools*
The moose likes Java in General and the fly likes Overloading using List of Generic Types as Argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overloading using List of Generic Types as Argument" Watch "Overloading using List of Generic Types as Argument" New topic
Author

Overloading using List of Generic Types as Argument

Ronwaldo Cruz
Ranch Hand

Joined: Oct 17, 2006
Posts: 69
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
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ronwaldo Cruz
Ranch Hand

Joined: Oct 17, 2006
Posts: 69
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

Joined: Jan 30, 2000
Posts: 18671
Yes - but you can use instanceof on the things within the List, to see if each is an AA or AB, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading using List of Generic Types as Argument