Why can't assign I <? extends Type> to ?

后端 未结 2 1128
半阙折子戏
半阙折子戏 2020-12-11 18:22

The following statements:

URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class uclc = ucl.getClass();
         


        
2条回答
  •  旧时难觅i
    2020-12-11 18:40

    Covariance vs contravariance vs invariance

    • Class is invariant.

    As a result,

    Class is not a subtype of Class


    In Java a variable can hold a reference of an instance of same type or subtype.

    Hence,

    Class uclc = ucl.getClass();
    

    is invalid.

    On the other hand,

    Class uclc = ucl.getClass();
    

    would be valid.

提交回复
热议问题