Govt. Exams
Entrance Exams
Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.
java
List
With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.
You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.
public T findMin(T[] arr)
Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).
List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).
Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.
java
List strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.
Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.
Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.
public void sort(T[] array)
With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.