Skip to content

Commit b900137

Browse files
committed
feat(generics): add GenericClass2 with multiple type parameters <T,U>
WHAT: - Implemented a generic class `Test2<T,U>` that supports two independent type parameters. - Added fields `obj1` and `obj2` of types `T` and `U` respectively. - Provided constructor for initialization and a `print()` method to display values. - Created `GenericClassDemo2` with a demo instantiation: `Test2<String,Integer>`. WHY: - Demonstrates how Java Generics can handle multiple type parameters. - Shows how to design reusable classes that store heterogeneous data while maintaining type safety. - Example builds on the idea of single type parameter classes like `Box<T>`. HOW: - Declared `class Test2<T,U>` with two type parameters. - `obj1` stores a value of type `T`, `obj2` stores a value of type `U`. - Constructor initializes both values. - `print()` outputs both stored objects. - In `main()`, instantiated `Test2<String,Integer>` with `"Multiple parameter"` and `15`. TYPE PARAMETER CONVENTIONS: - `<T, U>` are placeholder names: - `T` → Type (general placeholder) - `U` → Second independent type - Other common conventions: - `E` → Element (used in collections like `List<E>`) - `K` → Key - `V` → Value - `N` → Number REAL-WORLD USE CASES: - **Pairs/Tuples**: Represent related but different data types (e.g., name and ID). - **Database records**: Store column name (`String`) with its value (`Object` or typed). - **Key-value relationships**: Similar to `Map.Entry<K,V>` in Java Collections Framework. - **Generic utility classes**: For passing/returning heterogeneous values safely. NOTES: - Unlike raw `Object`-based storage, generics eliminate the need for casting. - Java’s `Map.Entry<K,V>` is a direct application of this pattern. - Further enhancements could include methods like `getFirst()` and `getSecond()` for readability. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e1e8b9e commit b900137

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
1+
package GenericClass2;
12
/*
23
We can also pass multiple Type parameters in Generic classes.
3-
4-
Java program to show multiple type parameters in Java Generics
5-
6-
We use < > to specify Parameter-type.
4+
Java program to show multiple type parameters in Java Generics, We use < > to specify Parameter-type.
75
*/
86

9-
class Test2<T, U>
10-
{
7+
class Test2<T, U> {
118
T obj1; // An object of type T
129
U obj2; // An object of type U
1310

1411
// constructor
15-
Test2(T obj1, U obj2)
16-
{
12+
Test2(T obj1, U obj2) {
1713
this.obj1 = obj1;
1814
this.obj2 = obj2;
1915
}
2016

2117
// To print objects of T and U
22-
public void print()
23-
{
18+
public void print() {
2419
System.out.println(obj1);
2520
System.out.println(obj2);
2621
}
2722
}
2823

29-
class GenericClassDemo2
30-
{
31-
public static void main (String[] args)
32-
{
24+
class GenericClassDemo2 {
25+
public static void main (String[] args) {
3326
Test2<String, Integer> obj = new Test2<>("Multiple parameter", 15);
3427
obj.print();
3528
}
36-
}
29+
}

0 commit comments

Comments
 (0)