Commit df81c09
committed
docs(generics): add comprehensive guide on wildcards, generic methods, and type bounds
WHAT:
- Added detailed documentation on advanced Java Generics concepts:
- Wildcards (? , ? extends T , ? super T)
- Generic methods and constructors
- Upper-bounded and lower-bounded wildcards
- Bounded type parameters (<T extends Number>)
- Type erasure (compile-time vs runtime behavior)
- Generic interfaces and classes
- Included multiple code examples with outputs to illustrate usage.
- Added a summary comparison table for quick reference.
DETAILS:
- Explained wildcards:
- ? → unbounded, allows any type (read-only).
- ? extends T → safe for reading subtypes (covariant).
- ? super T → safe for writing supertypes (contravariant).
- Documented generic methods with <T> type parameters, supporting multiple data types.
- Showcased bounded type parameters to enforce constraints on generic types.
- Illustrated type erasure by comparing generic classes at runtime (ArrayList<String> vs ArrayList<Integer>).
- Added generic interface and implementation (e.g., GenericInterface<T>).
- Demonstrated generic constructors with flexible type instantiation.
WHY:
- Provides a single reference file consolidating core and advanced Generics concepts.
- Helps prevent common mistakes (e.g., adding elements to ? extends T collections).
- Enhances understanding of polymorphism, variance, and type safety in collections and custom data structures.
- Supports real-world use in APIs, frameworks (Spring, Hibernate), and data modeling.
EXAMPLES ADDED:
- Wildcard usage in method arguments (printList(List<?>)).
- Sum of numbers using upper bounds.
- Adding integers using lower bounds.
- Type erasure demonstration with ArrayList.
- Generic class (Box<T>), method (<T> void display), and constructor.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e48cdc1 commit df81c09
File tree
1 file changed
+24
-35
lines changed- Section24JavaGenerics/src/WildcardsInGenerics
1 file changed
+24
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
43 | | - | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
49 | | - | |
| 48 | + | |
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
| |||
73 | 72 | | |
74 | 73 | | |
75 | 74 | | |
76 | | - | |
77 | 75 | | |
| 76 | + | |
78 | 77 | | |
79 | 78 | | |
80 | 79 | | |
81 | 80 | | |
82 | | - | |
83 | | - | |
| 81 | + | |
84 | 82 | | |
85 | 83 | | |
86 | 84 | | |
| |||
106 | 104 | | |
107 | 105 | | |
108 | 106 | | |
109 | | - | |
110 | | - | |
| 107 | + | |
| 108 | + | |
111 | 109 | | |
112 | 110 | | |
113 | 111 | | |
114 | | - | |
115 | | - | |
| 112 | + | |
116 | 113 | | |
117 | 114 | | |
118 | 115 | | |
119 | | - | |
120 | | - | |
| 116 | + | |
121 | 117 | | |
122 | 118 | | |
123 | 119 | | |
| |||
139 | 135 | | |
140 | 136 | | |
141 | 137 | | |
142 | | - | |
| 138 | + | |
143 | 139 | | |
144 | 140 | | |
145 | 141 | | |
146 | 142 | | |
147 | | - | |
148 | | - | |
| 143 | + | |
149 | 144 | | |
150 | 145 | | |
151 | | - | |
152 | | - | |
| 146 | + | |
153 | 147 | | |
154 | 148 | | |
155 | 149 | | |
| |||
176 | 170 | | |
177 | 171 | | |
178 | 172 | | |
179 | | - | |
| 173 | + | |
180 | 174 | | |
181 | 175 | | |
182 | 176 | | |
183 | 177 | | |
184 | 178 | | |
185 | 179 | | |
186 | | - | |
| 180 | + | |
187 | 181 | | |
188 | 182 | | |
189 | 183 | | |
| |||
194 | 188 | | |
195 | 189 | | |
196 | 190 | | |
197 | | - | |
| 191 | + | |
198 | 192 | | |
199 | 193 | | |
200 | | - | |
201 | 194 | | |
| 195 | + | |
202 | 196 | | |
203 | 197 | | |
204 | | - | |
205 | | - | |
| 198 | + | |
206 | 199 | | |
207 | 200 | | |
208 | 201 | | |
| |||
230 | 223 | | |
231 | 224 | | |
232 | 225 | | |
233 | | - | |
234 | | - | |
| 226 | + | |
235 | 227 | | |
236 | 228 | | |
237 | | - | |
238 | | - | |
| 229 | + | |
239 | 230 | | |
240 | 231 | | |
241 | 232 | | |
| |||
251 | 242 | | |
252 | 243 | | |
253 | 244 | | |
254 | | - | |
255 | | - | |
| 245 | + | |
256 | 246 | | |
257 | 247 | | |
258 | 248 | | |
259 | | - | |
260 | | - | |
| 249 | + | |
261 | 250 | | |
262 | 251 | | |
263 | | - | |
| 252 | + | |
264 | 253 | | |
265 | 254 | | |
266 | 255 | | |
| |||
272 | 261 | | |
273 | 262 | | |
274 | 263 | | |
275 | | - | |
| 264 | + | |
276 | 265 | | |
277 | 266 | | |
278 | 267 | | |
279 | 268 | | |
280 | 269 | | |
281 | 270 | | |
282 | | - | |
| 271 | + | |
0 commit comments