Generics in java at method level

Viewed 49

I am new to java. I am trying to debug a code and not able to understand one line.

public interface CommandDispatcher {
    <T extends BaseCommand> void registerHandler(Class<T> type, CommandHandlerMethod<T> handler);
    void send(BaseCommand command);
}

I know generics but not able to understand below line.

 <T extends BaseCommand> void registerHandler(Class<T> type, CommandHandlerMethod<T>)

what is <T extends BaseCommand> before void also I am not ablr to understand Class<T>

Can somebody explain me to understand the above line. Consider BaseCommand is an interface.

1 Answers

<T extends BaseCommand> means at the calling side Type T can be BaseCommand OR derived from BaseCommand class/interface.

Class<T> type means the first argument should be the type of Class T.

class Command extends BaseCommand{ 
}

then you can call like this

registerHandle( Command.class, ...);

So that inside implementation one can create instance of Type Command.

Related