Commit 8b93867
committed
feat(generic-interface): add wildcard support in generic interface methods
WHAT:
- Introduced `WildcardExample` class with `printContainer(Container<?>)` method.
- Uses an unbounded wildcard `?` to allow passing a `Container` of any type.
- Prints the stored item regardless of its underlying type.
WHY:
- Demonstrates the flexibility of **wildcards** in generic interfaces.
- Without wildcards, `Container<Object>` is not compatible with `Container<String>` or `Container<Integer>`.
- Wildcards make methods more general and reusable across varying type parameters.
HOW (Logic Flow):
1. Define `printContainer(Container<?>)` that accepts any `Container` regardless of its type parameter.
2. Call `container.get()` safely (result treated as `Object`).
3. Print the contained item to demonstrate compatibility.
BENEFITS:
- Increases **API flexibility** by allowing different generic instantiations to be passed.
- Preserves type safety while avoiding unsafe casts.
- Simplifies utility methods that should operate across multiple type variants.
REAL-WORLD APPLICATIONS:
- Logging or debugging containers without knowing their parameterized type.
- Utility frameworks (collections, serialization, UI rendering) where handling different generic types uniformly is required.
- Example: `List<?>` in the Java Collections Framework is used widely for read-only operations across different list types.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5190c15 commit 8b93867
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
0 commit comments