-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericBoxExample.java
More file actions
45 lines (37 loc) · 1.25 KB
/
GenericBoxExample.java
File metadata and controls
45 lines (37 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* This is a Java program demonstrating the use of Generics in a class.
* Generics allow us to create a class that can work with different data types.
*/
// Define a generic class with a type parameter <T>
class GenericBox<T> {
private T value; // A variable of type T
// Constructor to initialize the value
public GenericBox(T value) {
this.value = value;
}
// Method to get the value
public T getValue() {
return value;
}
// Method to set a new value
public void setValue(T value) {
this.value = value;
}
// Method to display the value
public void display() {
System.out.println("Value: " + value);
}
}
public class GenericsExample {
public static void main(String[] args) {
// Create a GenericBox instance for Integer type
GenericBox<Integer> intBox = new GenericBox<>(100);
intBox.display(); // Output: Value: 100
// Create a GenericBox instance for String type
GenericBox<String> strBox = new GenericBox<>("Hello, Generics!");
strBox.display(); // Output: Value: Hello, Generics!
// Change the value
strBox.setValue("Updated String Value");
strBox.display(); // Output: Value: Updated String Value
}
}