views:

76

answers:

2

is it possible somehow to accept only super types of the generic Type of a class?

what im looking for is something like:

class <T extends Object> MyClass {

   public <TS super T> void myMethod(TS someObjectSuperToTheClass) { //do stuff }

}

i don't really need it anymore (and its probably not all that usefull to begin with) but I'm curious if this is at all possible and if not why.

cheers, P.V. Goddijn

+2  A: 

No you cannot! Only wildcards can have a super bound.

notnoop
+4  A: 

Think about what it would mean in this case.

You want to assert that TS is either T, or any of its superclasses. But since TS is merely a reference, the actual someObjectSuperToTheClass parameter can be TS or a subclass.

Putting both halves together, in comes out that your code is entirely equivalent to

public void myMethod(Object someObjectSuperToTheClass) { //do stuff }

since together, you've got that TS can walk as high as it wants up the class hierarchy, and the parameter can walk down as far as it wants.

What was it you were trying to constrain the parameter to, with this syntax?

Andrzej Doyle
in hindsight i wanted to constrain the argument to having at least on shared interface. but thats of course always the case.i guess i wanted TS super T 'but not Object itself' :)
pvgoddijn
and thanks for explaining why it would make no sense IF it was possible
pvgoddijn