Java using generics with lists and interfaces

后端 未结 6 1790
遇见更好的自我
遇见更好的自我 2021-01-13 11:02

Ok, so here is my problem:

I have a list containing interfaces - List a - and a list of interfaces that extend that interface: Li

6条回答
  •  渐次进展
    2021-01-13 12:06

    You can't do that and be safe because List and List are different types in Java. Even though you can add types of SubInterface to a list of Interface, you can't equate the two lists with different interfaces even if they're sub/super interfaces of eachother.

    Why is it that you want to do b = a so bad? Do you just want to store a reference to the SubInterface list?

    On a side note, I suggest you read this documentation on the oracle site: http://download.oracle.com/javase/tutorial/java/generics/index.html It explains and goes deep into generics very well.

提交回复
热议问题