Java Wildcards Interview Questions and Answers
Learn Java Wildcards with production-ready interview questions, ?, ? extends, ? super, PECS, covariance, contravariance, Collections examples, and enterprise use cases.
Java Wildcards Interview Questions & Answers
Introduction
Wildcards are one of the most confusing topics in Java Generics and are frequently asked in Senior Java Developer interviews.
A wildcard represents an unknown type. Instead of specifying an exact generic type, Java allows us to write flexible APIs using the ? symbol.
Wildcards are extensively used throughout the Java Collections Framework and enterprise frameworks such as Spring, Hibernate, and Jackson.
Understanding Unbounded Wildcards, Upper Bounded Wildcards, Lower Bounded Wildcards, and the PECS Principle is essential for writing reusable and type-safe APIs.
1. What are Wildcards in Java?
Answer
A Wildcard represents an unknown generic type.
Syntax
List<?>
Instead of accepting only one type,
List<String>
we can accept any generic type.
List<?>
Examples
List<Integer>
List<String>
List<Employee>
All are valid.
Wildcards increase the flexibility of generic methods.
2. Why do we need Wildcards?
Answer
Suppose we have a method.
public void print(List<Object> list){
}
Can we pass
List<String>
No.
Because
List<String>
!=
List<Object>
Instead we write
public void print(List<?> list){
}
Now the method accepts
- List
- List
- List
This makes the API reusable.
3. What is an Unbounded Wildcard?
Answer
An unbounded wildcard accepts any generic type.
Syntax
List<?>
Example
public void display(List<?> list){
for(Object obj : list){
System.out.println(obj);
}
}
Valid
display(List<String>)
display(List<Integer>)
display(List<Double>)
Since the compiler doesn't know the element type, you can safely read elements as Object, but you cannot add arbitrary elements (except null).
4. What is an Upper Bounded Wildcard?
Answer
An upper bounded wildcard restricts types to a superclass or interface.
Syntax
<? extends Number>
Example
public double sum(
List<? extends Number> numbers){
double total = 0;
for(Number n : numbers){
total += n.doubleValue();
}
return total;
}
Valid
List<Integer>
List<Double>
List<Float>
Invalid
List<String>
Upper bounds are ideal when the collection is a producer of data.
5. What is a Lower Bounded Wildcard?
Answer
A lower bounded wildcard specifies the minimum accepted type.
Syntax
<? super Integer>
Example
public void addNumbers(
List<? super Integer> list){
list.add(10);
list.add(20);
}
Valid
List<Integer>
List<Number>
List<Object>
Invalid
List<Double>
Lower bounds are useful when the collection is a consumer of data.
6. What is the difference between extends and super?
Answer
extends
Read data.
List<? extends Number>
You can safely read Number values.
super
Write data.
List<? super Integer>
You can safely insert Integer values.
Comparison
| extends | super |
|---|---|
| Producer | Consumer |
| Read | Write |
| Upper Bound | Lower Bound |
| Flexible Reading | Flexible Writing |
This distinction forms the basis of the PECS principle.
7. What is the PECS Principle?
Answer
PECS stands for:
Producer Extends, Consumer Super
If a collection produces values, use:
<? extends T>
If a collection consumes values, use:
<? super T>
Example
copy(
List<? extends Number> source,
List<? super Number> destination
)
This is one of the most frequently asked interview questions on Generics.
8. Can we add elements to List<?>?
Answer
No.
Example
List<?> list =
new ArrayList<String>();
Invalid
list.add("Java");
Compiler Error.
The compiler does not know the actual element type.
Only
list.add(null);
is allowed.
This restriction maintains type safety.
9. What is the difference between List<Object> and List<?>?
Answer
Many developers confuse these two.
List
Can store any object.
List<Object>
You can add:
- String
- Integer
- Employee
List<?>
Unknown type.
Cannot safely add objects.
Comparison
| List | List<?> |
|---|---|
| Any Object | Unknown Type |
| Add Allowed | Add Not Allowed |
| Read Object | Read Object |
| Mutable | Read-only for arbitrary additions |
They serve different purposes and are not interchangeable.
10. Where are Wildcards used in the Collections Framework?
Answer
Java Collections heavily use wildcards.
Example
Collections.copy(
List<? super T>,
List<? extends T>)
Another example
Collections.max(
Collection<? extends T>)
These APIs are flexible because of wildcard usage.
11. Explain covariance and contravariance.
Answer
Covariance
Uses
? extends
Allows reading.
Example
List<? extends Animal>
Can reference
- Dog
- Cat
- Tiger
Contravariance
Uses
? super
Allows writing.
Example
List<? super Dog>
Can accept
- Dog
- Puppy
These concepts make generic APIs safer.
12. Explain a production use case of Wildcards.
Answer
Scenario
A reporting service exports data from different entity types.
Entities
- Employee
- Customer
- Product
Method
public void export(
List<?> records){
}
The same export logic now works for all entity types.
Another example is a utility method that copies data between collections.
Result
- Reusable APIs
- Less duplicate code
- Better maintainability
- Improved type safety
Wildcards help build framework-level utilities that work across many object types.
13. What are the advantages of Wildcards?
Answer
Advantages include:
- Flexible APIs
- Better code reuse
- Compile-time type safety
- Cleaner generic methods
- Reduced casting
- Better compatibility with Collections
- Easier library development
Wildcards are heavily used in enterprise frameworks and utility libraries.
14. What are the best practices for using Wildcards?
Answer
Recommended practices:
- Use
?when the exact type is unimportant. - Use
? extendsfor read-only producers. - Use
? superfor write-only consumers. - Avoid unnecessary wildcard complexity.
- Prefer meaningful generic APIs.
- Follow the PECS principle.
- Keep wildcard usage simple and well documented.
Following these practices improves API readability and usability.
15. What interview tips should you remember about Wildcards?
Answer
Interviewers commonly ask:
- What is a wildcard?
- Difference between
extendsandsuper. - What is PECS?
- Can we add elements to
List<?>? - Difference between
List<Object>andList<?>. - Covariance vs Contravariance.
- Collections examples.
- Production use cases.
Remember
?means unknown type.? extends→ Read (Producer).? super→ Write (Consumer).List<Object>is not the same asList<?>.- Wildcards make generic APIs flexible and reusable.
- The Java Collections Framework extensively uses wildcards.
Summary
Wildcards are a powerful feature of Java Generics that enable flexible, reusable, and type-safe APIs. Mastering unbounded, upper bounded, and lower bounded wildcards, along with the PECS principle, is essential for understanding the Java Collections Framework and writing enterprise-grade generic code.
Key Takeaways
- Understand the purpose of wildcards.
- Learn the difference between
?,? extends, and? super. - Master the PECS principle.
- Know when to use upper and lower bounded wildcards.
- Understand covariance and contravariance.
- Learn why
List<Object>differs fromList<?>. - Recognize wildcard usage in the Collections Framework.
- Design reusable generic APIs using wildcards.
- Follow wildcard best practices.
- Support interview answers with practical production examples.