Skip to content

Commit 5190c15

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

1 file changed

+1
-1
lines changed

Section24JavaGenerics/src/GenericInterface/StringContainer1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public String get() {
1111
return item;
1212
}
1313
}
14-
//In this example, StringContainer implements the Container interface with String as the specified type parameter.
1514

15+
//In this example, StringContainer implements the Container interface with String as the specified type parameter.
1616

1717
/*
1818
class GenericContainer<T> implements Container<T> {

0 commit comments

Comments
 (0)