Specify that a Class argument must implement a particular interface

user149408

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?

user149408

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 */
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Interface's implementations MUST implement its inner class builder interface. How to do it?

From Dev

If a class is using an interface, it must

From Dev

How do I create a List of Class objects, all of which implement a particular interface?

From Dev

Member variable which must extend class A and implement some interface

From Dev

How to implement a interface function in a class

From Dev

Interface/class/implement/extend confusion

From Dev

RestyGWT content argument must be a class

From Dev

Parse error : Must specify a ParseObject class name

From Dev

Implement an interface in partial class

From Dev

Implement Serializable to a class implementing an Interface

From Dev

Force derived class to implement interface

From Dev

Is there a way to specify or assert that the child class must re-implement a specific non-abstract virtual method?

From Dev

DLang - Template constraints - type must implement interface

From Dev

Catchable Fatal Error: Argument 1 passed to "...\FormType::__construct() must implement interface

From Dev

Specify implement interface on derived class when base class implements it abtract

From Dev

Must a class adhere to the documented contract of an interface to be said to implement that interface

From Dev

How to specify particular setter for property of a class?

From Dev

Can I specify that a particular tag always have a particular class?

From Dev

If a class is using an interface, it must

From Dev

Member variable which must extend class A and implement some interface

From Dev

Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to [...]::__construct() must implement interface [...] none given

From Dev

Java: type parameter must implement interface

From Dev

What does the class type constraint achieve if a generic type constraint must also implement an interface in c#

From Dev

How to implement this particular class hierarchy?

From Dev

Implement an interface in partial class

From Dev

Declaration of class must be compatible with interface

From Dev

Laravel ::__construct() must implement interface

From Dev

Implement interface in class or as member

From Dev

Argument must extend class

Related Related

  1. 1

    Interface's implementations MUST implement its inner class builder interface. How to do it?

  2. 2

    If a class is using an interface, it must

  3. 3

    How do I create a List of Class objects, all of which implement a particular interface?

  4. 4

    Member variable which must extend class A and implement some interface

  5. 5

    How to implement a interface function in a class

  6. 6

    Interface/class/implement/extend confusion

  7. 7

    RestyGWT content argument must be a class

  8. 8

    Parse error : Must specify a ParseObject class name

  9. 9

    Implement an interface in partial class

  10. 10

    Implement Serializable to a class implementing an Interface

  11. 11

    Force derived class to implement interface

  12. 12

    Is there a way to specify or assert that the child class must re-implement a specific non-abstract virtual method?

  13. 13

    DLang - Template constraints - type must implement interface

  14. 14

    Catchable Fatal Error: Argument 1 passed to "...\FormType::__construct() must implement interface

  15. 15

    Specify implement interface on derived class when base class implements it abtract

  16. 16

    Must a class adhere to the documented contract of an interface to be said to implement that interface

  17. 17

    How to specify particular setter for property of a class?

  18. 18

    Can I specify that a particular tag always have a particular class?

  19. 19

    If a class is using an interface, it must

  20. 20

    Member variable which must extend class A and implement some interface

  21. 21

    Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to [...]::__construct() must implement interface [...] none given

  22. 22

    Java: type parameter must implement interface

  23. 23

    What does the class type constraint achieve if a generic type constraint must also implement an interface in c#

  24. 24

    How to implement this particular class hierarchy?

  25. 25

    Implement an interface in partial class

  26. 26

    Declaration of class must be compatible with interface

  27. 27

    Laravel ::__construct() must implement interface

  28. 28

    Implement interface in class or as member

  29. 29

    Argument must extend class

HotTag

Archive