List of everything that implements such a class

Asked

Viewed 42 times

1

Well I wanted to know why I can’t create a list of classes that implement another class, for example

public List<Class<? implements Classe>> classes; this returns a syntax error...

But I can create one of classes that extend from another class...

public List<Class<? extends Classe>> classes; returns no error...

  • You mean, implement an interface, right?

1 answer

0


Although the keyword is the same, extends in a generic statement works for both class and interface. That is, you can create a list of everything that implements an interface, in the same way that creates a list of everything you inherit from a class:

interface CommandExecutor { }

class Foo implements CommandExecutor { }
class Bar implements CommandExecutor { }

public List<Class<? extends CommandExecutor>> commandExecutos =
    new ArrayList<Class<? extends CommandExecutor>>();

Example in the ideone (same example with classes).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.