Generics by Durga Sir
Generics by Durga Sir
Generics by Durga Sir
1. Introduction
2. Generic classes
3. Bounded types
4. Generic methods & wild card character (?)
5. Communication with non-generic code
6. conclusions
Introduction:
Definition: The main objective of Generics is to provide Type-
Safety and to resolve Type-Casting problems.
Case 1: Type-Safety
Arrays are always type safe that is we can give the
guarantee for the type of elements present inside array.
For example if our programming requirement is to hold String
type of objects it is recommended to use String array. In
the case of string array we can add only string type of
objects by mistake if we are trying to add any other type we
will get compile time error.
That is we can always provide guarantee for the type of elements
present inside array and hence arrays are safe to use with
respect to type that is arrays are type safe.
But collections are not type safe that is we can't provide any
guarantee for the type of elements present inside collection.
For example if our programming requirement is to hold only
string type of objects it is never recommended to go for
ArrayList.
By mistake if we are trying to add any other type we won't get
any compile time error but the program may fail at runtime.
Case 2: Type-Casting
Concluson2:
classArrayList
add(Object o);
Example:
Example:
classArrayList<String>
add(String s);
Example:
class Account<T>
{}
class Gen<T> {
T obj;
Gen(T obj) {
this.obj = obj;
}
public T getObject() {
return obj;
}
}
System.out.println(g1.getObject());
System.out.println(g2.getObject());
System.out.println(g3.getObject());
}
}
Output:
10
Akshay
10.5
Bounded types:
We can bound the type parameter for a particular range by using
extends keyword such types are called bounded types.
Example 1:
class Test<T>
{ }
Here as the type parameter we can pass any type and there are no
restrictions hence it is unbounded type.
Example 2:
{}
Example 2:
Example:
But implements keyword purpose we can replace with extends
keyword.
As the type parameter we can use any valid java identifier but it
convention to use T always.
Example:
Example:
Example 1:
As the type parameter we can pass any type which extends Number class
Example 3:
Example 4:
Example 5:
Example:
l.add("A");
l.add(null);
l.add(10);//(invalid)
Within the method we can add only String type of objects and null to
the List.
2. m1(ArrayList<?> l):
We can use this method for ArrayList of any type but within the
method we can't add anything to the List except null.
Example:
l.add(null);//(valid)
l.add("A");//(invalid)
l.add(10);//(invalid)
}
Declaring type parameter at method level:
We have to declare just before return type.
class Test
}
}
Example:
8. Public <T extends Number & Comparable & Runnable> void me4(T t)
{}//valid
package collectionDemo;
import java.util.ArrayList;
class Test {
Conclusions:
Example 1:
package collectionDemo;
import java.util.ArrayList;
class Test {
}
}
Example 2:
importjava.util.*;
class Test
Output:
m1(java.util.ArrayList<java.lang.Integer>)
Example:
l1.add("A");//valid
l1.add(10); //invalid