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

Lecture 7 - Generic Class and Method

The document discusses the concept of generics in Java, including generic classes and methods, and their advantages such as type safety and eliminating the need for multiple method overloads. It explains the implementation of generic methods and classes, including bounded type parameters and wildcards. The content also includes examples and conventions related to generics, along with a quiz for practical application.

Uploaded by

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

Lecture 7 - Generic Class and Method

The document discusses the concept of generics in Java, including generic classes and methods, and their advantages such as type safety and eliminating the need for multiple method overloads. It explains the implementation of generic methods and classes, including bounded type parameters and wildcards. The content also includes examples and conventions related to generics, along with a quiz for practical application.

Uploaded by

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

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Generic Classes and Methods

(IT069IU)

Nguyen Trung Ky

📧 [email protected]

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

-> Use one 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 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

● Type parameter meaning


○ E: Element type in a collection

○ K: Key type in a map

○ V: Value type in a map

○ T: General type

○ S, U: Additional general types

12
Implementing a Generic Methods
• Generic methods are methods that introduce their own type parameters.

• The type parameter's scope is limited to the method where it is declared.

• The syntax for a generic method includes a type parameter, inside angle
brackets, and appears before the method's return type.

public <T> void printArray( T[] inputArray )


{
//body
} // end method printArray
Quiz 1
- Try to solve this Generic problem in Java on HackerRank website:
https://www.hackerrank.com/challenges/java-generics/problem?isFullScreen=true

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

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

19
Simple Generic Class Example

20
Another Example of Generic Class

21
Single Type Vs Multiple Type Parameters

Single Type Parameter

Multiple Type Parameter


22
Bounded Type
Parameters

Limit the type parameters to specific group of subclass!

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

The ? stands for an unknown type


? extends Type : a bounded wildcard. Type is upper bound
? super Type : a bounded wildcard. Type is lower bound
Wildcards

• The question mark (?), called the wildcard, represents an


unknown type.
• The wildcard can be used in a variety of situations: as the
type of a parameter, field, or local variable.
Wildcards Demo.
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.

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

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

38
Thank you for your listening!

“Motivation is what gets you started.


Habit is what keeps you going!”
Jim Ryun

39

You might also like