Generics - Handouts
Generics - Handouts
Generics
Lecture – 7
OOP
Contents
• Wrapper classes
• Introduction to Generics
• Generic classes - Declaration & Instantiation
• Generic Methods – Implementation & Invocation
• Bounded Type Parameters
• Wildcards
• Limitations in Generics
OOP
1
3/19/19
OOP
autoboxing.java
4
OOP
unboxing.java
5
OOP
Generics in Java
• Concept of “generics” was introduced in JDK 5 to
deal with type-safe objects.
2
3/19/19
OOP
OOP
Advantages of Generics
1. Type-safety : We can hold only a single type of
objects in generics. It doesn’t allow to store other
types of objects.
2. Type casting is not required: There is no need to
typecast the object. All type conversions are
implicit.
3. Compile-Time Checking: It is checked at compile
time so problem will not occur at runtime. The
good programming strategy says it is far better to
handle the problem at compile time than at
runtime.
8
OOP
Implementation
• When you are in need to store a list of values, you
shall use an array.
e.g.: if you are storing marks of 5 students then you may
create an array as shown in the code below
3
3/19/19
OOP
Implementation cont.
Problem:
• Size of the array is fixed. If there is a change in the
size of the array (in the number of elements that
are stored), you have to modify the code manually.
• You may not be able to expand or shrink the array
automatically as & when the element is being
added.
Solution:
• Use Collection Interface
10
OOP
Implementation cont.
Shown below is an example of how Collection is used
to store elements. Note that the list grows each &
every time add() is called.
GenericDemo2.java
11
OOP
Implementation cont.
Collection value = new ArrayList();
• This statement allows you to create a list of
elements (list of marks).
• Note that Collection is an interface and cannot be
instantiated directly.
• ArrayList is a class which implements the Interface
List which extends the Interface Collection.
12
4
3/19/19
OOP
Implementation cont.
Collection value = new ArrayList();
Problem:
This list may contain any object as we have not
specified the data type of the element to be added.
13
OOP
Implementation cont.
Collection value = new ArrayList();
Problem:
What if the requirement is to store only integers?
Solution:
Make use of Generics!
Collection <Integer> value = new ArrayList<>();
OOP
Implementation cont.
Collection <Integer> value = new ArrayList<>();
• Now you may not be able to add elements other
than Integer type. (Note the Compilation Error)
GenericDemo3.java
15
5
3/19/19
OOP
Question:
Write a program to store a list of names, retrieve the
names & display on the screen.
16
OOP
Type Parameters
The type parameter naming conventions are as
follows:
T - Type
E - Element
K - Key
N - Number
V - Value
17
OOP
Generic class
• A class that can refer to any type is known as generic class.
• Type T indicates that it can refer to any type (Integer,
Double, Employee etc.)
test.java
18
6
3/19/19
OOP
OOP
Generic method
• Like generic class, we can create generic method that can
accept any type of argument.
test1.java
20
OOP
Multiple Parameters
• A generic class or method can have multiple type
parameters.
test2.java
21
7
3/19/19
OOP
test2.java
22
OOP
OOP
8
3/19/19
OOP
test3.java
25
OOP
test3.java
26
OOP
27
9
3/19/19
OOP
28
OOP
• If we write <? extends Number>, it means any child class of Number (e.g.
Integer, Double, Float etc.) can be matched with the ?
test4.java
29
OOP
test4.java
30
10
3/19/19
OOP
Limitations in Generics
• Type parameters cannot be instantiated - It is not possible to create
instances of a type parameter
11