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<?>?
- 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<? extends Number> accept List<Integer>?
- No
- Yes
- Only List<Number>
- Only List<Object>
Q8. Is List<String> a subtype of List<Object>?
- Yes
- No
- Sometimes
- Always
Q9. Is List<? super Integer> 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!