Skip to content

Commit 4ff824d

Browse files
committed
feat(generic-interface): add generic Container interface with concrete and generic implementations
WHAT: - Introduced a generic interface `Container<T>` with two core operations: - `add(T item)` → store an item. - `T get()` → retrieve the stored item. - Added two implementations: 1. `StringContainer` → fixed to work only with `String` type. 2. `GenericContainer<T>` → fully generic, supports any type at instantiation. - Added `Main` class to demonstrate usage with `String` and `Integer`. WHY: - To illustrate how generic interfaces enable type-safe contracts that can be implemented differently: - Specialized implementation (e.g., `StringContainer`). - Reusable generic implementation (e.g., `GenericContainer<T>`). - Avoids boilerplate and unsafe casting while maintaining flexibility. HOW (Logic Flow): 1. Define `Container<T>` interface → abstraction with generic type parameter `T`. 2. Implement with `StringContainer` (type fixed) and `GenericContainer<T>` (fully reusable). 3. Demonstrate instantiation: - `StringContainer` stores and retrieves `String`. - `GenericContainer<Integer>` stores and retrieves an `Integer`. BENEFITS: - Provides compile-time type safety. - Eliminates the need for explicit casting when retrieving values. - Increases reusability by supporting both specific and generic use cases. REAL-WORLD APPLICATIONS: - Mirrors patterns in Java Collections API (`List<T>`, `Set<T>`, `Map<K,V>`). - Useful in dependency injection, caching, and data processing frameworks. - Can serve as the foundation for building strongly typed containers for domain-specific logic. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 57f610b commit 4ff824d

File tree

1 file changed

+6
-7
lines changed
  • Section24JavaGenerics/src/GenericInterface

1 file changed

+6
-7
lines changed

Section24JavaGenerics/src/GenericInterface/Main.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package GenericInterface;
22

3-
// Define a generic interface
3+
// Define a generic interface.
44
interface Container<T> {
55
void add(T item);
66
T get();
77
}
88

9-
// Specific implementation for String
9+
// Specific implementation for String.
1010
class StringContainer implements Container<String> {
1111
private String item;
1212

@@ -19,7 +19,7 @@ public String get() {
1919
}
2020
}
2121

22-
// Generic implementation for any type
22+
// Generic implementation for any type.
2323
class GenericContainer<T> implements Container<T> {
2424
private T item;
2525

@@ -36,20 +36,19 @@ public T get() {
3636

3737
public class Main {
3838
public static void main(String[] args) {
39-
// Using StringContainer
39+
// Using StringContainer.
4040
StringContainer strContainer = new StringContainer();
4141
strContainer.add("Hello, Generics!");
4242
System.out.println(strContainer.get());
4343

44-
// Using GenericContainer
44+
// Using GenericContainer.
4545
GenericContainer<Integer> intContainer = new GenericContainer<>();
4646
intContainer.add(42);
4747
System.out.println(intContainer.get());
4848
}
4949
}
50-
//In this example, StringContainer implements the Container interface with String as the specified type parameter.
51-
5250

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

5453
/*
5554
class GenericContainer<T> implements Container<T> {

0 commit comments

Comments
 (0)