Lecture 10 - Generic Class
Lecture 10 - Generic Class
International University
School of Computer Science and Engineering
(IT069IU)
🌐 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.
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:
- Use that generic class to create an object from it and specific what datatype
will be replace T.
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
21
Bounded Type
Parameters
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
26
Advantages of Java Generics
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!
30