Commit bb7ffe9
committed
docs(generics): add detailed explanation and examples for lower-bounded wildcards (? super T)
WHAT:
- Introduced the concept of lower-bounded wildcards using `? super T`.
- Explained syntax (`? super T` → "unknown type that is a superclass of T").
- Added examples demonstrating how lists declared with lower bounds can safely accept elements of a specific type or its subclasses.
- Covered real-world use cases like adding elements to collections and consumer-producer patterns.
- Documented restrictions (cannot retrieve specific elements beyond `Object`, cannot add unrelated types).
WHY:
- Lower-bounded wildcards are often confusing for developers compared to upper-bounded wildcards.
- They provide flexibility when consuming (writing to) collections while preserving type safety.
- Understanding their limitations helps avoid runtime errors and improves correct API design.
DETAILS:
- `addNumbers(List<? super Integer>)` → accepts `List<Integer>`, `List<Number>`, or `List<Object>`.
- `addToCollection(List<? super Number>)` → allows adding `Integer` and `Double` since both extend `Number`.
- `printNumbers(List<? super Integer>)` → demonstrates safe reading (but only as `Object`).
- Restrictions documented:
- Cannot safely retrieve elements except as `Object`.
- Cannot add unrelated types like `String` when bound is `? super Number`.
BENEFITS:
- Encourages correct use of the PECS principle (Producer Extends, Consumer Super).
- Provides safe and flexible ways to handle collections when the exact type is unknown but bounded by a lower limit.
- Enhances clarity on when to use `? super` vs `? extends`.
REFERENCE:
- PECS Principle:
- Producer → `? extends T` (read-only, covariant).
- Consumer → `? super T` (write-only, contravariant).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9c215df commit bb7ffe9
File tree
1 file changed
+75
-0
lines changed- Section24JavaGenerics/src/LowerBoundedWildcards
1 file changed
+75
-0
lines changedLines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
0 commit comments