Podcast
Questions and Answers
What type of wildcard is written as Collection<?>?
What type of wildcard is written as Collection<?>?
- Bounded Wildcard
- Unbounded Wildcard (correct)
- Unrestricted Wildcard
- Unknown Wildcard
What type is List<? extends Shape>?
What type is List<? extends Shape>?
- Bounded Wildcard (correct)
- Unbounded Wildcard
- Unrestricted Wildcard
- Unknown Wildcard
What type is List<Shape>?
What type is List<Shape>?
- Bounded Wildcard
- Unbounded Wildcard
- Unrestricted Wildcard (correct)
- Unknown Wildcard
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?
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?
Flashcards are hidden until you start studying
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.