0% found this document useful (0 votes)
4 views5 pages

CH-15 Generics

The document discusses the importance of generics in programming, specifically for providing type safety and resolving type casting issues in collections. It explains the differences between arrays and collections regarding type safety, type casting, and the introduction of generics in Java to address these problems. Additionally, it covers the concept of bounded types, generic classes, methods, and the use of wildcards in generics, along with valid and invalid declarations.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

CH-15 Generics

The document discusses the importance of generics in programming, specifically for providing type safety and resolving type casting issues in collections. It explains the differences between arrays and collections regarding type safety, type casting, and the introduction of generics in Java to address these problems. Additionally, it covers the concept of bounded types, generic classes, methods, and the use of wildcards in generics, along with valid and invalid declarations.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

[**DURGA SIR -79-95 AND HK VOLUME2 PAGE-248**]

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);

this type type casting is mandatory in collection.

Note:- To overcome above prob of collection(type safety, and type casting) sun
introduced generics concepts in 1.5v

Q-Why generics are used


ans:-
1 To provide type safety to collections
2 To resovle type casting problums

**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.

means Arraylist can hold ref of its sub type.

ex:

=> List<String> l = new ArrayList<String>(); // correct


=> Collection<String> al = new ArrayList<String>();// correct
=> ArrayList<Object> al = new ArrayList<String>();// its not allowed Object

**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

ex: List<int> l = new ArrayList<int>(); // not allowed pritive type param.

**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.

Imp-Note: so through generic we are getting type-safety.


and at the time of retrival its not req to perform any type casting we
can assigned directally only defined type values.

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
{

class Test<T super String> // incorrect


{

Note: we can pass any no of type param , need not be one.


ex:
class HashMap<k,v>

Note:-we can also define in combination also.


ex: HashMap<Integer, String> h = new HashMap<Integer, String>();
or
ex: class Test<T extends Number & Runnable>
{ // we can pass any type which extends Number class and impl Runnable
interface.
}

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.
{
}

** GENERIC METHODS AND WILD CARD CHARACTOR(?):


==============================================

CASE1: method1(ArrayList<String> al);


note:- this mehtod applicable only for string type. it allowed to add string and
null only.

CASE1: method1(ArrayList<?> al);


note:- we use this mehtod for ArrayList of any type but within the method we can
not add anything to list except null.
this mehtod is usefull only when we are performing read operation.

CASE3: mehtod1(ArrayList< ? extends X > al);


if x is class then this mehtod applicable for ArrayList of either x type
or its child classes.
if x is interface then this mehtod is applicable for ArrayList of either x
type or super class of impl class of x.

Note : we can declare type param either class level or mehtod level.
** VALID OR INVALID DECLARATIONS:
---------------------------------
1 ArrayList<String> al = new ArrayList<String>(); //valid

2 ArrayList<?> al = new ArrayList<String>(); //valid

3 ArrayList<?> al = new ArrayList<Integer>(); //valid

4 ArrayList< ? extends Number> al = new ArrayList<Integer>(); //valid

5 ArrayList< ? extends Number> al = new ArrayList<String>(); //invalid

Note : we can declare type param either class level or mehtod level.
in mehtod level declare type param befor return type.

ex: public <T> void m1(T t) //valid

public <T extends Number> void m1(T t) //valid

** CONCLUSION:
--------------
> Generic concept is applicable only at compile time , at runtime no such concept .

You might also like