0% found this document useful (0 votes)
17 views11 pages

Generics - Handouts

Uploaded by

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

Generics - Handouts

Uploaded by

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

3/19/19

Object Oriented Programming

Generics
Lecture – 7

SLIIT - Faculty of Computing

OOP

Contents
• Wrapper classes
• Introduction to Generics
• Generic classes - Declaration & Instantiation
• Generic Methods – Implementation & Invocation
• Bounded Type Parameters
• Wildcards
• Limitations in Generics

SLIIT - Faculty of Computing

OOP

Wrapper class / Covering class


• Wrapper class in java provides the mechanism to convert
primitive datatype into object and object into primitive type.

• For each primitive datatype, there exists a covering class in the


java.lang package.
byte à Byte
short à Short
int à Integer
long à Long
float à Float
double à Double
char à Character
Booleanà Boolean 3

SLIIT - Faculty of Computing

1
3/19/19

OOP

Wrapper class cont.


• The automatic conversion of primitive type into object is
known as autoboxing and vice-versa unboxing.

• Since J2SE 5.0, autoboxing and unboxing feature converts


primitive into object and object into primitive automatically.

autoboxing.java
4

SLIIT - Faculty of Computing

OOP

Wrapper class cont.


unboxing

unboxing.java
5

SLIIT - Faculty of Computing

OOP

Generics in Java
• Concept of “generics” was introduced in JDK 5 to
deal with type-safe objects.

• Introduction of generics has changed Java in 2


ways:
• Added new syntax to Java language
• Caused changes to many of the classes & methods in the
core API

• With the use of generics, it is possible to create


classes, interfaces & methods which works in type-
safe manner.
6

SLIIT - Faculty of Computing

2
3/19/19

OOP

Generics in Java cont.


• “Generics” means “parameterized types”.

• Many algorithms are same regardless of its data


type that is applied. With generics, you can define
the algorithm once independently of any specific
type of data. Later, you can use the algorithm to a
wide variety of data types without any additional
effort.

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

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

*Make use of Wrappers as primitive types do not support


Generics.
14

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

5
3/19/19

OOP

Question:
Write a program to store a list of names, retrieve the
names & display on the screen.

Hint: Use an ArrayList class

16

SLIIT - Faculty of Computing

OOP

Type Parameters
The type parameter naming conventions are as
follows:
T - Type
E - Element
K - Key
N - Number
V - Value

17

SLIIT - Faculty of Computing

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

SLIIT - Faculty of Computing

6
3/19/19

OOP

Generic class cont.


• You may make use of the generic class as shown below:

• Note that there will be a compilation error if you attempt to


add any other type of data other than an integer.
test.java
19

SLIIT - Faculty of Computing

OOP

Generic method
• Like generic class, we can create generic method that can
accept any type of argument.

test1.java
20

SLIIT - Faculty of Computing

OOP

Multiple Parameters
• A generic class or method can have multiple type
parameters.

test2.java
21

SLIIT - Faculty of Computing

7
3/19/19

OOP

Multiple Parameters cont.


• This is how you will make use of the generic class you
created:

test2.java
22

SLIIT - Faculty of Computing

OOP

Multiple Parameters cont.


• Although the two type argument differ in the example, it is
possible for both types to be same.
e.g.:

• Also note, if both type arguments were always the same,


then two type parameters would be unnecessary. 23

SLIIT - Faculty of Computing

OOP

Bounded Type Parameters


• There may be times when you will want to restrict the kind
of data types that are allowed to be passed to a type
parameter.
e.g.: A method that operates on numbers might only want to
accept instances of Number or its subclasses.
• To declare a bounded type parameter, list the type
parameter's name, followed by the extends keyword,
followed by its upper bound in the following format.
<T extends superclass>
e.g.: <T extends Number>
• Note: T, can only be replaced by the superclass or its
subclasses.
24

SLIIT - Faculty of Computing

8
3/19/19

OOP

Bounded Type Parameters cont.


• This is an example of the class with a bounded type
parameter:

test3.java
25

SLIIT - Faculty of Computing

OOP

Bounded Type Parameters cont.


• This is how you will make use of the class with a bounded
type parameter.

test3.java
26

SLIIT - Faculty of Computing

OOP

Bounded Type Parameters cont.


• Note that the following code segment will give compilation
error as String is not a subclass of Number

27

SLIIT - Faculty of Computing

9
3/19/19

OOP

Bounded Type Parameters cont.


• In addition to using a class type as a bound, you can also use
an interface type too! In fact, you can specify multiple
interfaces as bounds.
• A bound can include a class type & one or more interfaces.
In this case, the class type needs to be specified first.
• When a bound include an interface type, only type
arguments that implement that interface are legal.
• When specifying a bound that has a class & an interface or
multiple interfaces, use the & operator to connect them
e.g:

28

SLIIT - Faculty of Computing

OOP

Wildcard in Java Generics


• The ? Symbol represents the wildcard element. It means any type can be
matched with the ? symbol.

• 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

SLIIT - Faculty of Computing

OOP

Wildcard in Java Generics cont.

test4.java
30

SLIIT - Faculty of Computing

10
3/19/19

OOP

Limitations in Generics
• Type parameters cannot be instantiated - It is not possible to create
instances of a type parameter

• Restriction on static members - No static member can use a type


parameter declared by the enclosing class. Note that you can declare
static generic methods.

• Generic array restriction -


• Cannot instantiate an array whose element type is a type
parameter.
• Cannot create an array of type specific references

• Generic exception restriction - A generic class cannot extend


Throwable. This means we cannot create generic exception classes. 31

SLIIT - Faculty of Computing

11

You might also like