Why is Collection not simply treated as Collection<?>

后端 未结 4 1635
猫巷女王i
猫巷女王i 2021-01-01 09:03

Consider the following API method taken from Shiro\'s org.apache.shiro.subject.PrincipalCollection interface but probably present in other libraries as well:



        
4条回答
  •  [愿得一人]
    2021-01-01 09:12

    The reason is quite simple:

    You may read Objects from a Collection the same way as from Collection. But you can't add Objects to a Collection (The compiler forbids this) whereas to a Collection you can.

    If after the release of Java 5 the compiler had translated every Collection to Collection, then previously written code would not compile anymore and thus would destroy the backward compatibility.

提交回复
热议问题