Why does this compile?

前端 未结 2 1500
伪装坚强ぢ
伪装坚强ぢ 2021-02-18 23:00

I was taken aback earlier today when debugging some code to find that something like the following does not throw a compile-time exception:

 public Test () { 
         


        
相关标签:
2条回答
  • 2021-02-18 23:48

    For one thing List is an interface. There is no reason why there couldn't exist a subclass of HashMap which also implements the List interface. In this situation it would be perfectly valid.

    0 讨论(0)
  • 2021-02-18 23:53

    Because conceivably getList() could be returning a subclass of HashMap which also implements List. Unlikely, yes, but possible, and therefore compilable.

    0 讨论(0)
提交回复
热议问题