Gil Steidle wrote:I tried flipping the logic and used "extends" in place of "super" to get the classes Apple or lower. This doesn't work. I thought it would. What am I still missing?
This means that it will only allow you to add objects that IS-A Apple, IS-A Macintosh, IS-A GrannySmith, and IS-A anything else that IS-A Apple.
The only possible item that can be added, because it can be assigned to all of those reference types, is null.
Gil Steidle wrote:
List<? super Apple> b // Declare a List that will accept a List higher than Apple
b = new ArrayList<Fruit>(); // Give it a List<Fruit>
//b.add(new Fruit()); // Try to add a Fruit object to a List<Fruit> fails, why???
Paul Anilprem wrote:I think you are over complicating it in your mind. It is quite simple actually, if you understand the following three basic principles:
Paul Clapham wrote:
If you want a List which can contain anything which is a subclass of Fruit, then List<Fruit> is what you want. You can add any Fruit to that.