0

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?

1
  • by the way there is no need for T -- the declaration boolean isOK ( Class<? extents I> cl ) would be equivalent Commented Jul 15, 2018 at 1:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.