Commit 5190c15
committed
feat(generic-interface): implement Container<String> with StringContainer1
WHAT:
- Added `StringContainer1` class implementing the generic `Container<String>` interface.
- Provides type-safe storage and retrieval of `String` values.
- Encapsulates a `String` item with:
- `add(String item)` → stores the string.
- `get()` → retrieves the stored string.
WHY:
- Demonstrates a **concrete implementation** of a generic interface with a fixed type (`String`).
- Highlights how generics can be narrowed down when implementing to enforce strict type usage.
HOW (Logic Flow):
1. Define `StringContainer1` that implements `Container<String>`.
2. Implement `add()` to assign a `String` value to the private field.
3. Implement `get()` to return the stored `String`.
BENEFITS:
- Type safety: only `String` values can be added (compile-time enforcement).
- No need for type casting when retrieving values.
- Clear demonstration of how a generic interface can be specialized for a single type.
REAL-WORLD APPLICATIONS:
- Useful in cases where a container is designed for one specific type (e.g., message queues holding only text).
- Mirrors patterns in frameworks where type constraints are locked at implementation to reduce misuse.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4ff824d commit 5190c15
File tree
1 file changed
+1
-1
lines changed- Section24JavaGenerics/src/GenericInterface
1 file changed
+1
-1
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | 14 | | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
0 commit comments