CH-15 Generics
CH-15 Generics
INTRODUCTION:
-------------
the main objective of Generics is to
1 provide Type-Safety and
2 resolve Type-Casing problums.
CASE 1: Type-Safety
-------------------
> array are always type safe.
> ex : if our prog req is to hold string type of objects its recommended to use
string array.
in case of string array , we can hold only string type of objects, if by
mistak we try to add any other then get CE.
Note:-
> array are type safe.
but
> collections are not type-safe.[means we can not say which type elements present
inside colleciton]
ex: if req to hold string type of objects then its never recomended to for for
arraylist.
by mistake if we try to add any other type then we will not get any CE but prog
may fail at Runtime.
CASE 2: Type-Casting:
---------------------
> in case of array at the time of retrivel , its not req to perform any type
casting.
ex: String sname=str[0];
but
> in case of collection, at the time of rerival compulsory req type casting else we
get CE.
ex: String s=(String) al.get(0);
Integer i = (Integer) al.get(1);
Note:- To overcome above prob of collection(type safety, and type casting) sun
introduced generics concepts in 1.5v
**To hold only perticular type objects we can create a generic version of arraylist
as below:
ex: ArrayList<String> al = new ArrayList<String>();
Note:we can store only defined type objects. like above string only.
Imp-Note: after defining a generic type object , while retriving object type
casting not required.
this is done through generic syntax.
**CONCLUSION 1:
===============
Note:- Polymorphism concept is applicable only for base type not for param type.
[holding child ref in parent]
ex: ArrayList<String> // here Arraylist is base type and String is param type.
ex:
**CONCLUSION 2:
==============
> Collections concept applicabe only for objects, so for param type we can use any
class or interface but not primitive type.
if use then we get CE
**GENERIC CLASSES:
==================
in 1.5 v a generic version of ArrayList class is declared as follows.
ex:
class ArrayList<T>
{
add(T t);
T get(int index)
}
Note:- based on our req T will be replaced with our provide type.
ex:
class ArrayList<String>
{
add(String s);
String get(int index);
}
Note: add() mehtod can take only string type as argument so we can add only string
type objects to list.
and if we try to add another type then we get CE.
Note: based on our req we can create our own generic classes also.
ex: class Account<T>
{
Accoutn<Gold> g1 = new Accoutn<Gold>();
Accoutn<Silver> s1 = new Accoutn<Silver>();
}
** BOUNDED TYPES:
================
we can bound the type param for a perticular range by using extends kw , such type
called bounded types.
ex:
class Test<T> // we can pass any type and there is no restrictions, its
unbounded type.
{
ex:2
class Test<T extends X> //if X is class, then we can pass as a const param
either X or its child.
{ //if X is interface, then we can pass as a const param either
X or its implemented class.
}
ex:3
class Test<T extends Integer>
{
}
class Test2
{
PSVM()
{
Test<Ineger> t1 = new Test<Integer>();
}
}
ex:4
class Test<T extends Runnable>
{
}
class Test2
{
PSVM()
{
Test<Thread> t1 = new Test<Thread>();
}
}
Imp-Note:- we can not define bounded types by using implements and super kw.
see below examples
class Test<T implements Runnable> // incorrect
{
Note:
ex: class Test<T extends Number & String> // incorrect:- we can not extends more
than one class at a time.
{ // but at a time two interfaces are allowed.
}
Note:
ex: class Test<T extends Runnable & Number> // first should be class.
{
}
Note : we can declare type param either class level or mehtod level.
** VALID OR INVALID DECLARATIONS:
---------------------------------
1 ArrayList<String> al = new ArrayList<String>(); //valid
Note : we can declare type param either class level or mehtod level.
in mehtod level declare type param befor return type.
** CONCLUSION:
--------------
> Generic concept is applicable only at compile time , at runtime no such concept .