Map with values that are subclasses of keys?

How would one declare a Map whose values should be subclasses of its keys?

Naively, I thought I could do something like this (this fails, of course):

private <Textends SomeClass, Vextends T> Map<T, V> map;

Is this impossible?

Thanks,

Laird

[389 byte] By [ljnelsona] at [2007-10-2 21:46:06]
# 1
I don't think you can declare types in member declarations like that. Not sure though.If you put the declaration for T and V in the class declaration, it works fine.
dubwaia at 2007-7-14 1:01:41 > top of Java-index,Core,Core APIs...
# 2

> I don't think you can declare types in member

> declarations like that. Not sure though.

Yeah, you certainly can't for fields. You can? maybe? for methods? I seem to recall doing this, but can't be sure either.

> If you put the declaration for T and V in the class

> declaration, it works fine.

Yep. Unfortunately this field is a cache that backs another method. The class itself should not be constrained by these type parameters, and the caller should never really see them.

Oh well; another day, another generics shortcoming. :-)

Cheers,

Laird

ljnelsona at 2007-7-14 1:01:41 > top of Java-index,Core,Core APIs...
# 3

> Yep. Unfortunately this field is a cache that backs

> another method. The class itself should not be

> constrained by these type parameters, and the caller

> should never really see them.

I'm having a hard time imagining what code will set the parameters to something concrete? What would do that, a subclass?

dubwaia at 2007-7-14 1:01:41 > top of Java-index,Core,Core APIs...
# 4
I've seen a long and interesting thread on this topic. The conclusion was very short: it's impossible.
Maaartina at 2007-7-14 1:01:41 > top of Java-index,Core,Core APIs...