Skip to content

Commit 29e28a5

Browse files
committed
feat(generic-methods): demonstrate method overloading with generics and specific types
WHAT: - Added `MethodOverloading` class to explore how **generic methods** interact with **method overloading**. - Used `printArray(T[])` from `Test` class (via static import) to print arrays of different types (`Integer[]`, `String[]`). - Implemented two `display` methods: 1. A generic version `<T> void display(T element)`. 2. A type-specific version `void display(Integer element)`. WHY: - Shows how **Java resolves overloaded methods** when both a generic and a specific method exist. - Emphasizes that the **most specific method** (non-generic) is chosen by the compiler when applicable. HOW (Logic Flow): 1. Call `printArray()` for integers and strings, proving reusability of a single generic method. 2. Call `display(12)` → matches the `Integer`-specific method. 3. Call `display(12.1)` → no exact match, so compiler falls back to the generic version. OUTPUT BEHAVIOR: - `printArray(intArray)` → prints integer elements. - `printArray(stringsArray)` → prints string elements. - `display(12)` → "Integer Display: 12" - `display(12.1)` → "Generic display: 12.1" BENEFITS: - Demonstrates **method resolution precedence** between generics and overloaded methods. - Reinforces the flexibility of generic methods without losing the optimization of type-specific overloads. - Shows practical use cases of mixing polymorphism + generics. REAL-WORLD APPLICATIONS: - API design: providing both generic fallbacks and optimized type-specific implementations. - Framework utilities: e.g., `Collections.sort()` has generic forms, but some libraries offer specialized overloads for performance. - Logging/debugging frameworks where generic loggers coexist with specialized ones for primitives or common types. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 444e45f commit 29e28a5

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package GenericMethods;
2+
3+
import static GenericMethods.Test.printArray;
4+
5+
public class MethodOverloading{
6+
public static void main(String[] args) {
7+
Integer[] intArray = {1,2,3,4};
8+
String[] stringsArray = {"Hello","World"};
9+
printArray(intArray);
10+
printArray(stringsArray);
11+
display(12);
12+
display(12.1);
13+
}
14+
public static <T> void display(T element) {
15+
System.out.println("Generic display: "+ element);
16+
}
17+
18+
public static void display(Integer element) {
19+
System.out.println("Integer Display: "+ element);
20+
}
21+
}
22+
23+
/*
24+
1. Generic Methods:
25+
- `public static <T> void display(T element)`
26+
- Yaha `<T>` ek **type parameter** hai jo compile-time pe decide hota hai.
27+
- Matlab tum koi bhi type ka element pass kar sakte ho (Integer, Double, String, etc.).
28+
29+
2. Method Overloading + Generics:
30+
- Do methods hain:
31+
a) `public static <T> void display(T element)` → generic method.
32+
b) `public static void display(Integer element)` → specifically Integer ke liye overload.
33+
- Java compiler **overloading resolution** ke time pe sabse specific method choose karta hai.
34+
- Agar tum `display(12)` call karoge → Integer version call hoga.
35+
- Agar tum `display(12.1)` (Double) ya `display("Hello")` call karoge → generic version call hoga.
36+
37+
3. printArray Example:
38+
- Tumne `import static GenericMethods.Test.printArray;` kiya hai.
39+
- `printArray(intArray)` aur `printArray(stringsArray)` dono chal jaate hain, kyunki generic method arrays ke
40+
saath kaam karta hai:
41+
42+
public static <T> void printArray(T[] array) {
43+
for(T elem : array) System.out.println(elem);
44+
}
45+
```
46+
- Output me array ke elements print hote hain.
47+
48+
4. Example Flow (MethodOverloading.java):
49+
- `display(12);` → Integer overload → "Integer Display: 12"
50+
- `display(12.1);` → Generic overload → "Generic display: 12.1"
51+
52+
5. Key Concept:
53+
- Overloaded methods ke beech agar ek exact match (non-generic) milta hai, toh wahi prefer hota hai.
54+
- Agar exact match nahi mila, toh generic method fallback hota hai.
55+
56+
✔ Generic methods allow type-safe reusability (`<T>`).
57+
✔ Overloading + generics → compiler sabse specific match choose karega.
58+
✔ Integer case → specific overload prefer hota hai.
59+
✔ Dusre types (Double, String) → generic method use hota hai.
60+
✔ `printArray` example → generic method arrays ke liye kaafi flexible hai.
61+
*/

0 commit comments

Comments
 (0)