Generics
_______________
_______________
Arrays are always safe wrt type. for example_ if our our program requirement is to add only String objects then we can go for String[] array. for this array we can add only String type of objects, if we are try to add any other type then we will get compiler time error.
String s = new String[111];
s[0] = "arjava";
s[1] = new Student(); // error: inCompaitable types found Student required String
Hence, in the case of arrays we can give the guarantee about the type of elements. String[] array contains only string objects, so arrays are always safe to use.
But, Collection is not safe to use wrt type, for example_ if our program requirement is to hold only String objects and if we use ArrayList, by mistake if we are trying to add any other type to the list then we won't get any compile time error but program may fail at run time.
ArrayList al = new ArayList();
al.add("arjava");
al.add(new Student());
String s1 = (String)al.get(0);
String s2 = (String)al.get(1); // error: RE classCastException
There is no guarantee that collection can hold a particular type of object, hence wrt type collection are not safe to use.
In the case of Arrays at the time of retrieval there is no required to perform TypeCasting, but in collection compulsory we have to provide TypeCasting otherwise we will get compile time error. to overcome of this collection problem (Type Safe and TypeCasting) generics concept is introduced.
Usage of parent class reference to hold child class object is considered as polymorphism, polymorphism concept is allowed only for base type not for parameter type. for parameter we can use only class name or interface name.
Until 1.4 version a non generic version of ArrayList class used to declare as follows -
class ArrayList {
add(object o);
Object get(int index)
}
In 1.5 version a generic version of ArrayList class use to declare as follows -
class ArrayList<T> {
add<T t>
T get(int index)
}
based on our requirement Type parameter 'T' will be replace with corresponding provided type. for example_ To hold only String type of object we have to create generic version of ArrayList object as follows.
ArrayList<String> al = new ArrayList();
we can pass any number of parameter, through generic we are associating a type parameter to the classes, such type of parameterized classes are called generic class.
Test<Integer> t1 = new Test<Integer>();
Test<String> t2 = new Test<String>(); // compile time error
we can bound the parameter in combination also
class Test<T extends Numbers & Runnable>
as the type parameter we can pass any type which is the child class of numbers and implements runnable interface
m1(ArrayList<String> al)
m2(ArrayList<? extends x> al)
m3(AL <? extends x> l)
m4(ArrayList <? super x> l)
Note: we can define the parameter either at class level or at method level
AL l = new AL();
AL l = new AL<String>();
AL l = new AL<interface>();
ArrayList l = new ArrayList<String>();
l.add("a");
l.add("10");
l.add("true");
System.out.println(l); //output - [a, 10, true]
These two declarations are equal
AL<String> l = new AL<String>();
AL<String> l = new AL();