Explain wildcard types in Java Generics

πŸ’‘ Concept: Wildcard Types in Generics

Wildcards in Java Generics use the ? symbol to represent unknown types. They make generic code more flexible by allowing you to work with related but not identical types.

πŸ“˜ Quick Intro

There are three main wildcard forms: ? (unbounded), ? extends T (upper-bounded), and ? super T (lower-bounded). They’re used to relax method parameter types while maintaining type safety.

🧠 Analogy

Think of ? extends T like accepting any fruit that’s a kind of Apple. ? super T is like accepting any container that can hold Apples. And ? means you’ll accept any random item regardless of type.

πŸ”§ Technical Explanation

  • List<?>: accepts any type of list (read-only).
  • List<? extends Number>: can read from a list of Number or its subclasses, but cannot write.
  • List<? super Integer>: can write Integer or its subclasses, but cannot read specific types.
  • Wildcard capture and PECS (Producer Extends, Consumer Super) principle guide their use.
  • Wildcards improve API design flexibility while preserving type safety.

🎯 Use Cases

  • βœ… API methods that operate on generic types without knowing exact type
  • βœ… Reading from generic producers (? extends)
  • βœ… Writing to generic consumers (? super)
  • βœ… Supporting multiple subclasses in method signatures

πŸ’» Example: Wildcard Types


import java.util.List;

public class WildcardExample {
    // Upper bounded wildcard (Producer)
    public static void printNumbers(List<? extends Number> list) {
        for (Number num : list) {
            System.out.println(num);
        }
    }

    // Lower bounded wildcard (Consumer)
    public static void addIntegers(List<? super Integer> list) {
        list.add(10);
        list.add(20);
    }
}

❓ Interview Q&A

Q1: What does ? represent in generics?
A: An unknown type.

Q2: When should you use ? extends T?
A: When you only need to read from the collection.

Q3: When should you use ? super T?
A: When you need to write to the collection.

Q4: What is PECS in Java generics?
A: Producer Extends, Consumer Super.

Q5: Can you add elements to List<? extends T>?
A: No.

Q6: Can you add to List<? super T>?
A: Yes.

Q7: Can List<?> accept any type?
A: Yes, but you can’t add to it.

Q8: Are wildcards optional in generics?
A: Yes, but they add flexibility.

Q9: Does List<Object> accept List<String>?
A: No, generics are not covariant.

Q10: Can wildcards be used in method return types?
A: Yes, but cautiously.

πŸ“ MCQs

Q1. What does '?' mean in generics?

  • Any class
  • Generic list
  • Unknown type
  • Null

Q2. What does '? extends T' allow?

  • Write to list
  • Only null
  • Read from list
  • Nothing

Q3. What does '? super T' allow?

  • Only read
  • Write to list
  • Only sort
  • Type erasure

Q4. Can you add to List&lt;?&gt;?

  • Yes
  • No
  • Only once
  • Only null

Q5. Which principle explains wildcard usage?

  • SOLID
  • PECS
  • POJO
  • MVC

Q6. What is '?' used for?

  • Interface
  • Default type
  • Wildcard type
  • Class type

Q7. Can List&lt;? extends Number&gt; accept List&lt;Integer&gt;?

  • No
  • Yes
  • Only List&lt;Number&gt;
  • Only List&lt;Object&gt;

Q8. Is List&lt;String&gt; a subtype of List&lt;Object&gt;?

  • Yes
  • No
  • Sometimes
  • Always

Q9. Is List&lt;? super Integer&gt; a consumer?

  • No
  • Yes
  • Never
  • Only static

Q10. Can you use wildcard in method return?

  • No
  • Yes
  • Only public
  • Only void

πŸ’‘ Bonus Insight

Use β€œextends” when you're only reading from a structure and β€œsuper” when you're only writing. Think β€œProducer Extends, Consumer Super” (PECS) to decide which wildcard to use.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

Learn More About Java β˜•

What is Java and its key features πŸ‘‰ Explained
Explain the Java Virtual Machine (JVM) πŸ‘‰ Explained
Difference between JDK, JRE, and JVM πŸ‘‰ Explained
What are Java’s main data types πŸ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java πŸ‘‰ Explained
What is the difference between a class and an object πŸ‘‰ Explained
Explain encapsulation with an example πŸ‘‰ Explained
What is inheritance in Java and its types πŸ‘‰ Explained
Define polymorphism in Java with examples πŸ‘‰ Explained
What is abstraction in Java πŸ‘‰ Explained
Difference between abstract class and interface in Java πŸ‘‰ Explained
Explain method overloading and method overriding in Java πŸ‘‰ Explained
What are constructors in Java πŸ‘‰ Explained
What is the use of the static keyword in Java πŸ‘‰ Explained
Explain the difference between final, finally, and finalize in Java πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup