1

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.

rashmi
  • 204
  • 1
  • 5
  • You want to research *generic methods*. Here is official tutorial: https://docs.oracle.com/javase/tutorial/extra/generics/methods.html. Also *bounded type* https://docs.oracle.com/javase/tutorial/java/generics/bounded.html – Pshemo Aug 13 '22 at 12:25

1 Answers1

1

<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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Sheel Prabhakar
  • 409
  • 2
  • 6