Something like this is what Im trying to achieve:
public Attribute<?> getAttribute(Class<? extends Attribute> attributeClass){
for(Attribute attribute: attributes)
if(attributeClass.isInstance(attribute)) return attributeClass.cast(attribute);
return null;
}
where the Attribute< ?> is obviously the incorrect part.
I want the return type of this method to be of attributeClass, but I cant seem to figure out how to get that. I should note that I am aware that I could just use Attribute as a return type, but this method would be mostly called in this fashion:
AttributeType1 attributeType1 = getAttribute(AttributeType1.class);
…and I am trying to avoid having to cast every time.
Help appreciated.
this is what you need
you need to specify the generic type before the return type