What is the general signature of the method returning an instance of this subclass?

Basically, I want to create a method that has the following signature:

public <T> T getShellTab(Class<T extends ShellTab> shellTabClass) 

but this is not true. Java I want to be able to pass a class that is a subclass of ShellTab , and have an instance of this class.

 public <T> T getShellTab(Class<T> shellTabClass) 

works fine, but I would like to make shellTabClass be a subclass of ShellTab .

Any ideas on how to do this?

Thanks.

+4
source share
2 answers

Put a constraint in the original generic parameter, for example:

 public <T extends ShellTab> T getShellTab(Class<T> shellTabClass) 

Please note that you may have restrictions on the parameters of the type type of the method parameters (for example, Tom Hawtin - tackline suggests making shellTabClass in Class<? extends T> , although I do not think that it matters in this case).
But you cannot restrict the type that has already been declared.

+12
source

How about this?

 public <T extends ShellTab> T getShellTab(Class<T> shellTabClass) 
+3
source

Source: https://habr.com/ru/post/1286627/


All Articles