Java Wildcard and Generic Types

Start Quiz

Study Flashcards

5 Questions

What type of wildcard is written as Collection<?>?

Unbounded Wildcard

What type is List<? extends Shape>?

Bounded Wildcard

What type is List<Shape>?

Unrestricted Wildcard

What type of type parameter is used for keys in the Map<K,V> generic type?

K

What type of type parameter is used for values in the Map<K,V> generic type?

V

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

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.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser