java - What is the practical application of an assignment involving a wildcard? -
what reason of assignment this?
list<? extends fruit> flist = new arraylist<apple>(); // flist.add(new apple()); // flist.add(new fruit()); // flist.add(new object());
once "upcast" apple container in fruit container, not able add in it.
i know like:
list<apple> basket = new arraylist<apple>(); //fill basket tons of juicy apples list<? extends fruit> fruitcontainer = basket;
and able use fruit interface use elements held fruitcontainer
. can practical reason this, if cannot add later?
list<? extends fruit> flist = new arraylist<apple>();
that line taken whole has no practical use, you've noted in question, each of the parts of (list<? extends fruit> flist
, new arraylist<apple>()
), separately, has practical use. they're both valid, , useful, not in combination (unless don't need put in list). that's why compiles, each of parts valid; it's us, programmers, combine them in reasonable , useful way.
Comments
Post a Comment