Thursday, June 18, 2015

JAVA - example of use in overrided method generic type of class that extends interface

public class HasLabelRenderer<T extends HasLabel> extends AbstractRenderer<T>{

    @Override    public String render(HasLabel object) {
        return object == null ? "" : object.getLabel();    }

}

This approach is possible due to the fact that in generics e.g. Bounded Type Parameter the type can be of the upper type it is extended. For instance, in example above the type T in HasLabelRenderer<T extends HasLabel>  can be of type HasLabel interface.

No comments: