r.Generics in Java
r.Generics in Java
Lecture - 25
Generics in java Templates in C++
Types
• Generic class
• Generic methods
Symbol to specify
Generic parameters <>
is ????
Syntax
{ {
Test(T obj) {
{ }
return this.obj; }
}
Generic Class – Example 1
class Test<T> class Main
{ {
Test(T obj) {
{ Now <T> will make the Test <Integer> Obj1 = new Test<Integer>(15);
{ }
So we can use the same class
return this.obj;
for integers and }Strings
} This isthan
rather Generic
writingclass
two Output:
} different classes 15
Face
Generic Class
Is it possible to pass
multiple Type parameters in
Generic classes ????
Generic Class
class Test<T, U> }
{ }
U obj2; {
{ {
• Code reusability