Monday, March 9, 2009




               In generics, an unknown type is represented by the wildcard character "?".

                           Cage someCage = ...;
                       Read "? extends Animal" as "an unknown type that is a subtype of Animal, possibly Animal itself", which boils down to "some kind of animal". This is an example of a bounded wildcard, where Animal forms the upper bound of the expected type. If you're asked for a cage that simply holds some kind of animal, you're free to provide a lion cage or a butterfly cage.

                        It's also possible to specify a lower bound by using the super keyword instead of extends. The code , therefore, would be read as "an unknown type that is a supertype of Animal, possibly Animal itself". You can also specify an unknown type with an unbounded wilcard, which simply looks like . An unbounded wildcard is essentially the same as saying .

                        you could house your animals in their individual cages, as shown earlier, and invoke this method first for the lions and then for the butterflies: 
Or, you could choose to combine your animals in the all-animal cage instead: 

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories

0 comments: to “ Wildcards

Extension Factory Builder