Skip to content

Commit df81c09

Browse files
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

1 file changed

+24
-35
lines changed

Section24JavaGenerics/src/Java Generics Wildcards, Generic Methods, and Super Bounds.txt renamed to Section24JavaGenerics/src/WildcardsInGenerics/Java Generics Wildcards, Generic Methods, and Super Bounds.txt

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
Java Generics: Wildcards, Generic Methods, and Super Bounds
22

3-
Java Generics provides flexibility and type safety.
3+
Java Generics provides flexibility and type safety.
44

55
Important concepts include:
6-
76
- Wildcards (?)
87
- Generic Methods
98
- Bounded Types (extends and super)
@@ -40,13 +39,13 @@ class WildcardExample {
4039
public static void main(String[] args) {
4140
List<String> stringList = List.of("Apple", "Banana", "Cherry");
4241
List<Integer> intList = List.of(1, 2, 3);
43-
42+
4443
printList(stringList);
4544
printList(intList);
4645
}
4746
}
4847

49-
Output:
48+
Output:
5049
Apple
5150
Banana
5251
Cherry
@@ -73,14 +72,13 @@ class GenericMethodExample {
7372
display(10.5); // Double
7473
}
7574
}
76-
✅ Output:
7775

76+
Output:
7877
100
7978
Hello
8079
10.5
8180

82-
✅ Key Points:
83-
81+
Key Points:
8482
- <T> before return type means T is a generic type.
8583
- The method can be called with any type.
8684

@@ -106,18 +104,16 @@ class UpperBoundExample {
106104
List<Integer> intList = List.of(1, 2, 3);
107105
List<Double> doubleList = List.of(1.1, 2.2, 3.3);
108106

109-
System.out.println(sumOfNumbers(intList)); // ✅ Works
110-
System.out.println(sumOfNumbers(doubleList)); // ✅ Works
107+
System.out.println(sumOfNumbers(intList));
108+
System.out.println(sumOfNumbers(doubleList));
111109
}
112110
}
113111

114-
✅ Output:
115-
112+
Output:
116113
6.0
117114
6.6
118115

119-
✅ Key Points:
120-
116+
Key Points:
121117
- ? extends Number means the method works with Integer, Double, Float, etc.
122118
- We cannot add elements to list (except null).
123119

@@ -139,17 +135,15 @@ class LowerBoundExample {
139135

140136
public static void main(String[] args) {
141137
List<Number> numList = new ArrayList<>();
142-
addNumbers(numList); // Works because Number is a superclass of Integer
138+
addNumbers(numList); // Works because Number is a superclass of Integer
143139
System.out.println(numList);
144140
}
145141
}
146142

147-
✅ Output:
148-
143+
Output:
149144
[10, 20, 30]
150145

151-
✅ Key Points:
152-
146+
Key Points:
153147
- ? super Integer means we can add Integer, but reading requires casting.
154148
- We cannot add Double, Float, etc.
155149

@@ -176,14 +170,14 @@ class BoundedExample<T extends Number> {
176170
}
177171
}
178172

179-
Key Points:
173+
Key Points:
180174

181175
- <T extends Number> means T can be Integer, Double, Float, etc.
182176
- Cannot use non-subclasses of Number (like String).
183177

184178
6️⃣ Type Erasure
185179

186-
Java removes generic type parameters at runtime.
180+
Java removes generic type parameters at runtime.
187181

188182
Example: Proof of Type Erasure
189183

@@ -194,15 +188,14 @@ class TypeErasureExample {
194188
ArrayList<String> list1 = new ArrayList<>();
195189
ArrayList<Integer> list2 = new ArrayList<>();
196190

197-
System.out.println(list1.getClass() == list2.getClass()); // ✅ True
191+
System.out.println(list1.getClass() == list2.getClass());
198192
}
199193
}
200-
✅ Output:
201194

195+
Output:
202196
true
203197

204-
✅ Key Points:
205-
198+
Key Points:
206199
- Java removes type parameters and treats all generic classes as Object at runtime.
207200
- Generic information only exists at compile-time.
208201

@@ -230,12 +223,10 @@ class Main {
230223
}
231224
}
232225

233-
✅ Key Points:
234-
226+
Key Points:
235227
- GenericInterface<T> allows different types to implement it.
236228

237-
8️⃣ Generic Constructors:
238-
229+
Generic Constructors:
239230
A constructor can also be generic.
240231

241232
Example: Generic Constructor:
@@ -251,16 +242,14 @@ class GenericConstructor {
251242
}
252243
}
253244

254-
✅ Output:
255-
245+
Output:
256246
Value: 10
257247
Value: Java
258248

259-
✅ Key Points:
260-
249+
Key Points:
261250
- <T> in a constructor allows it to accept different types.
262251

263-
📌 Summary Table
252+
Summary Table:
264253

265254
| Concept | Syntax | Use Case |
266255
|-----------------------------|-----------------------------|-----------------------------------------------|
@@ -272,11 +261,11 @@ Value: Java
272261
| Bounded Type Parameters | <T extends Number> | Restricts generic types to a certain hierarchy|
273262
| Type Erasure | Removes generics at runtime | Ensures backward compatibility |
274263

275-
🚀 Conclusion
264+
Conclusion
276265

277266
- Generics provide type safety and reusability.
278267
- Use wildcards for flexibility (? extends for reading, ? super for writing).
279268
- Use bounded type parameters (<T extends X>) for constraints.
280269
- Type erasure removes generics at runtime.
281270

282-
generic collections or custom data structures
271+
generic collections or custom data structures.

0 commit comments

Comments
 (0)