Lecture 7 - Generic Class and Method
Lecture 7 - Generic Class and Method
International University
School of Computer Science and Engineering
(IT069IU)
Nguyen Trung Ky
🌐 it.hcmiu.edu.vn/user/ntky
1
Previously,
- Polymorphism
- Method overriding and overloading in Inheritance
- Zoo Example
- Company Payroll Example
- Abstraction
- Abstract Class
- Abstract Method
- Why we need abstract class
- Examples:
- Zoo Example
- Company Payroll Example
- Interface
- Interface in real life examples
- Upgrade Company Payroll with Invoices Example
- Abstract vs Interface
2
Agenda’s today
- Generic
- Generic Method
- Generic Class
- Bounded type parameters
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.
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 repetitive!
10
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 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 (type parameter) by convention—then all three
methods would look like the one.
11
Conventions
○ T: General type
12
Implementing a Generic Methods
• Generic methods are methods that introduce their own type parameters.
• The syntax for a generic method includes a type parameter, inside angle
brackets, and appears before the method's return type.
14
With generic methods, we
can use only method to
handle different type of
data types instead of
creating many overloaded
method! Amazing!
15
Generic Class
17
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.
18
To create object/instance of Generic class
- Use that generic class to create an object from it and specific what datatype
will be replace T.
19
Simple Generic Class Example
20
Another Example of Generic Class
21
Single Type Vs Multiple Type Parameters
26
Problem
27
Solve it with Bound Type Parameters
Output:
28
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!
29
Bounded Type Parameters: Using wildcards
Object
36
Advantages of Java Generics
37
Recap
- Generic
- Generic Class
- Bounded type parameters
- Generic Method
38
Thank you for your listening!
39