Ilonpilaaja
Ilonpilaaja

Reputation: 1239

Java: Class-type parametrized by interface implementations

Let's say I have an interface

interface I {}

and two implementations thereof, class A implements I {} and class B implements I {} Now I would like to write a generic method which accepts a class-type parameter bounded by "Implements interface I", e.g.

boolean <T> isOK ( Class<T extents I> cl ) {
    switch ( cl ) {
       case A.class: return true ;
       case B.class: return false;
    }
}

How to do that?

Upvotes: 0

Views: 35

Answers (1)

khelwood
khelwood

Reputation: 59114

I think you mean something like:

public <T extends I> boolean isOK(Class<T> cl) {
   ...
}

The qualifiers for the generic type go at the point the generic type is declared, which is the first triangular brackets, not the second.

Also you can't switch on a Class, you'd have to use something else to examine it, such as an if statement.

Upvotes: 1

Related Questions