Java Wildcard and Generic Types
5 Questions
3 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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

  • Bounded Wildcard
  • Unbounded Wildcard (correct)
  • Unrestricted Wildcard
  • Unknown Wildcard
  • What type is List<? extends Shape>?

  • Bounded Wildcard (correct)
  • Unbounded Wildcard
  • Unrestricted Wildcard
  • Unknown Wildcard
  • 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?

    <p>K</p> Signup and view all the answers

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

    <p>V</p> 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.

    Quiz Team

    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.

    More Like This

    Use Quizgecko on...
    Browser
    Browser