0% found this document useful (0 votes)
7 views

Lecture 10 - Generic Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 10 - Generic Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Generic Classes and Methods

(IT069IU)

Le Duy Tan, Ph.D.


📧 [email protected]

🌐 leduytanit.com 1
Previously,
- Mid-term Exam Result
- Score Distribution
- Statistics
- Solutions
- Java Generic Collections
- Type-Wrapper Classes for Primitive Types
- Autoboxing vs Auto-unboxing
- List
- ArrayList
- Vector
- LinkedList
- Sets
- HashSet
- TreeSet
- Maps
- Hashtable
- HashMap
- TreeMaps
- HackerRank
- Introduction 2
- Coding Challenge
Agenda
- Generic
- Generic Class
- Bounded type parameters
- Generic Method

3
When does it start?

4
When does it start?

5
Generic
What problem does it solve?

6
The Problem

7
Generic
- Introduce type parameters for classes and methods, which are symbols can
be substituted for any concrete type.
- The benefit is to eliminate the need to create multiple versions of methods or
classes for various data types.

-> Use 1 version for all reference data types.

8
Generic Methods

9
This is the old way where we haven’t
learn about generic method where
we have to create a different
overloading methods with similar
bodies to handle different data
types. This is beyond ugly and
10
repetitive!
With generic methods, we
can use only method to
handle different type of
data types instead of
creating many overloaded
method! Amazing!
11
Motivation for Generic Methods
- Overloaded methods are often used to perform similar operations
on different types of data.
- Study each printArray method.
- Note that the type array element type appears in each method’s
header and for-statement header.
- If we were to replace the element types in each method with a
generic name—T by convention—then all three methods would
look like the one.

12
You can apply Generic in your game!

13
Generic Class

14
Generic Classes
- A Generic class simply means that the items or functions in
that class can be generalized with the parameter(example T)
to specify that we can add any type as a parameter in place
of T like Integer, Character, String, Double or any other user-
defined type.

15
To create object/instance of Generic class
- Create a new generic class with a type placeholder T:

public class ClassName <T>{



}

- Use that generic class to create an object from it and specific what datatype
will be replace T.

ClassName <Type> objectName = new ClassName <Type>();

16
Simple Generic Class Example

17
Another Example of Generic Class

18
ArrayList is actually a Generic Class

19
Single Type Vs Multiple Type Parameters

Single Type Parameter

Multiple Type Parameter


20
HashMap is actually a Generic Class
with multiple type parameters

21
Bounded Type
Parameters

Limit the type parameters to specific group of subclass!

22
Problem

23
Solve it with Bound Type Parameters

Output:

24
Bounded Type Parameters
By using the keyword “extends”,
we can force the type parameters
to have the requirement to be a
subclass of that super class. Like
in this example, the
MyNumberClass can only take
subclasses types of the superclass
Number!

25
Advantages of Java Generics

1. Type-Safety: One can hold only a single type of objects in generics.

2. Type Casting Is Not Required: There is no need to typecast.

26
Advantages of Java Generics

3. Compile -Time Checking: It checks all the errors of datatype related


to generics at the time of compile-time so the issue will not occur at
the time of runtime.

27
Your Turn
- Try to solve this Generic problem in Java on HackerRank website:
https://www.hackerrank.com/challenges/java-generics/problem?isFullScreen=true

28
Recap
- Generic
- Generic Class
- Bounded type parameters
- Generic Method

29
Thank you for your listening!

“Motivation is what gets you started.


Habit is what keeps you going!”
Jim Ryun

30

You might also like