Search code examples
javaclassinheritanceinterfaceimplements

Specify that a Class argument must implement a particular interface


I am writing a method which is passed a Class as an argument, like this:

public void doStuff(Class<?> clazz) {
    /* do stuff */
}

I can then call the method like this:

doStuff(FooBar.class);

How do I enforce at the compiler level that the argument can only be a class which implements a particular interface?

I’ve tried the following—

public <T extends FooInterface> void doStuff(Class<T> clazz) {
    /* do stuff */
}

—but get a compiler error for the following statement:

doStuff(FooBar.class); // FooBar implements FooInterface

How do I do this correctly?


Solution

  • Turns out the code I tried was in fact correct, and the compiler error (that is, Eclipse underlining the call in red) was a result of a missing try/catch block around the call.

    There are two possible ways to achieve the desired result:

    public <T extends FooInterface> void doStuff(Class<T> clazz) {
        /* do stuff */
    }
    

    Or, less elaborate:

    public void doStuff(Class<? extends FooInterface> clazz) {
        /* do stuff */
    }