Search code examples
javagenericsprotocol-bufferscovariancecontravariance

what does List<? extends SomeClass> mean?


I saw that definition in a protobuf generated java file:

java.util.List<? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder> foo();

But what dose <? and extends mean? I can understand List<SomeClass> I can't understand List<? extends SomeClass>..


Solution

  • This is a generic declaration.

    It is used to check types at compile time. You could put any object into a list, but that would make it more difficult to maintain and could cause ClassCastExceptions, if not used appropriate.

    <? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder> means "allow every class which extends DestInfoOrBuilder".