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:
Unsuccessful. Help.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
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???
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
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.
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|