Podcast
Questions and Answers
What type of wildcard is written as Collection<?>?
What type of wildcard is written as Collection<?>?
What type is List<? extends Shape>?
What type is List<? extends Shape>?
What type is List<Shape>?
What type is List<Shape>?
What type of type parameter is used for keys in the Map<K,V> generic type?
What type of type parameter is used for keys in the Map<K,V> generic type?
Signup and view all the answers
What type of type parameter is used for values in the Map<K,V> generic type?
What type of type parameter is used for values in the Map<K,V> generic type?
Signup and view all the answers
Study Notes
-
A wildcard type is a type whose element type matches anything.
-
A wildcard type is written Collection<?> (pronounced "collection of unknown"), and it is called a wildcard type for obvious reasons.
-
A wildcard type is a subtype of the unknown type Shape.
-
The type List<? extends Shape> is a bounded wildcard. This means that List<? extends Shape> is a subtype of both List<Shape> and Shape itself.
-
The type List<Shape> is an unrestricted wildcard. This means that List<Shape> is a subtype of any type.
-
The method drawAll() can only be called on lists of exactly Shape.
-
It is no longer legal to write into shapes in the body of the method.
-
For instance, this is not allowed:
You should be able to figure out why the code above is disallowed.
-
The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape.
-
Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
-
Map<K,V> is an example of a generic type that takes two type arguments, representing the keys and values of the map.
-
Again, note the naming convention for formal type parameters--K for keys and V for values.
-
Summarize the key facts from the text above in 10 sentences:
-
It is no longer legal to write into shapes in the body of the method.
-
For instance, this is not allowed:
You should be able to figure out why the code above is disallowed.
-
The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape.
-
Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
-
Map<K,V> is an example of a generic type that takes two type arguments, representing the keys and values of the map.
-
Again, note the naming convention for formal type parameters--K for keys and V for values.
-
Summarize the key facts from the text above in 10 sentences:
-
It is no longer legal to write into shapes in the body of the method.
-
For instance, this is not allowed:
You should be able to figure out why the code above is disallowed.
-
The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape.
-
Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
-
Map<K,V> is an example of a generic type that takes two type arguments, representing the keys and values of the map.
-
Again, note the naming convention for formal type parameters--K for keys and V for values.
-
Summarize the key facts from the text above in 10 sentences:
-
It is no longer legal to write into shapes in the body of the method.
-
For instance
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the concepts of Java wildcard and generic types, including bounded wildcards, generic type examples such as Map, and the naming convention for formal type parameters. It also explains the restrictions on writing into wildcard types and unknown subtypes of certain classes.