0

Went through a code snipped online

public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
        List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
        return authList;
    }

A. Can any please tell me what does ? signify?

B. <? extends GrantedAuthority> which class is extending GrantedAuthority?

C. In generics we provide the class type in <> to ensure type safety, why would anyone want to provide A extends B within <>?

underdog
  • 4,447
  • 9
  • 44
  • 89

1 Answers1

1

It is a wildcard which means any type that is extending Object(which also includes object).

So you can say that <?> is a shorthand for <? extends Object>

Check Oracle docs for Type Arguments and Wildcards

From here:

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331