Assuming that you have a lot of classes that extends the class Base.
class Base{}
class A extends Base[}
class B extends Base{}
class C extends Base{}
What must I write so that when I write a get method, I will get the class that I want?
public Base get(Class <? extends Base> clazz, final String key){
//not important.
}
I want the method to return Objects of class A, B, or C, depending on the input clazz.
asked Jun 11, 2017 at 15:36
theAnonymous
1,8244 gold badges34 silver badges70 bronze badges
1 Answer 1
public <T extends Base> T get(Class<T> clazz, final String key) {
}
is closer to what you need. Keep in mind that during method declared generic variables, to have more success you typically put the modifiers outside of the parameter list.
answered Jun 11, 2017 at 15:40
Edwin Buck
71.2k7 gold badges103 silver badges145 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Edwin Buck
Thanks Tom, in my hurry I forgot to add it.
lang-java