-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD4-10.txt
39 lines (34 loc) · 1.12 KB
/
D4-10.txt
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
class Record<E> {
private E record;
public void display(E item) {
System.out.println(item);
}
}
class Student {
private int studentId;
private String studentName;
public Student(int studentId,String studentName)
{
this.studentId=studentId;
this.studentName=studentName;
}
public String toString()
{
return "Student: Id = " + studentId + " Name = " + studentName;
}
}
class GenericsDemo {
public static void main(String[] args)
{
Student s1 = new Student(101,"Robert");
Record<Integer> integerRecord = new Record<Integer>(); //integerRecord can be used to display only integers
integerRecord.display(12);
//integerRecord.display(s1); will give an error as we are trying to add a student class object
Record<Student> studentRecord = new Record<>(); //studentRecord can be used to display only Students
studentRecord.display(s1);
//studentRecord.display(15); will give an error as we are trying to add an integer
}
}
Output:
12
Student: Id = 101 Name = Robert