Wildcard
Explore Java generics focusing on wildcard types. Understand how List<?> accepts only null for insertion and why extraction is limited to Object. Learn about the rules for lower bounds, their use as method arguments, and why they cannot be return types. This lesson equips you to handle wildcard generics confidently in interview scenarios.
We'll cover the following...
We'll cover the following...
1.
What is the ? used for in generics?
Show Answer
1 / 2
1.
Will the following method have worked in the previous question for printing a list of type Animal?
static <T extends Animal> void printAnimal(Collection<T> animals) {
for (Animal animal : animals)
animal.speakUp();
}
Show Answer
1 / 2
Technical Quiz
1.
What happens when we try to compile the following Java code?
Collection<?> myColl = new ArrayList<Object>();
myColl.add(new Object());
A.
Compiles
B.
Compiles with warnings
C.
Does not compile
1 / 1
The only element that you can ever insert into a ...