Skip to content

Commit 8b93867

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

File tree

1 file changed

+1
-1
lines changed
  • Section24JavaGenerics/src/GenericInterface

1 file changed

+1
-1
lines changed

Section24JavaGenerics/src/GenericInterface/Wild Cards

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class WildcardExample {
99
System.out.println(container.get());
1010
}
1111
}
12-
Here, the wildcard ? is used, meaning that printContainer can accept a Container of any type.
12+
Here, the wildcard ? is used, meaning that printContainer can accept a Container of any type.

0 commit comments

Comments
 (0)