Senior
How to modify the method signature to accept List<Integer>, List<Long>, List<Double> (all inheritors of Number)?
sobes.tech AI
Answer from AI
To make a method accept lists with elements of any subclass of the Number class (for example, Integer, Long, Double), you can use generics with an upper bounded wildcard:
public void processNumbers(List<? extends Number> numbers) {
for (Number num : numbers) {
System.out.println(num.doubleValue());
}
}
Here, List<? extends Number> means that the method accepts a list of elements of any type that is a subclass of Number. However, it is important to remember that with such a parameter, you cannot add elements to the list inside the method, as the exact type is unknown.