-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericClass.java
More file actions
59 lines (53 loc) · 1.37 KB
/
GenericClass.java
File metadata and controls
59 lines (53 loc) · 1.37 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package generic;
@SuppressWarnings("unchecked")
class Generic<List>{
List[] data = (List[]) new Object[5];
int length = 0;
void write(List s){
data[length++] = s;
}
void display(){
for(int i=0; i<length; i++){
System.out.println(data[i]);
}
}
}
class Data<Type>{
private Type Value;
void setData(Type Value){
this.Value = Value;
}
Type getData(){
return this.Value;
}
}
class MyArray<T>{
T[] A = (T[]) new Object[10];
int length = 0;
void append(T Value){
A[length++] = Value;
}
void display(){
for(int i=0; i<length; i++){
System.out.println(A[i]);
}
}
}
public class GenericClass {
public static void main(String[] args) {
Generic<String> GD = new Generic<>();
GD.write("Be hard on Your Routine");
GD.write("Finish Java ASAP");
// GD.write(100); // Integer not allowed, type not matched
GD.write("Now RUN");
GD.display();
Data<Integer> DI = new Data<>();
DI.setData(73);
System.out.println("Value: "+DI.getData());
MyArray<String> arr = new MyArray<>();
arr.append("Can only be written in the same class");
arr.append("Because Type of same Object");
arr.append("By using method for each task");
arr.display();
}
}