0% found this document useful (0 votes)
25 views27 pages

Lecture 11

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)
25 views27 pages

Lecture 11

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/ 27

Faculty of Information Technology - Programming 2

Lecture 11
Generic

Spring 2024 61FIT3PR2 – Programming 2 1


Generics Definition
• Generics is a programming approach that allows
an object to operate with multiple different data
types.
• The term "Generics" means type parameterization.
Type parameterization is crucial because it allows
us to create and use a class, interface, or method
with various data types.
• A class, interface, or method that operates on a
specified parameterized type is called generic.

Spring 2024 61FIT3PR2 – Programming 2 2


Non-Generics
• Example: Using ArrayList with different data
types

Retrieve:

Spring 2024 61FIT3PR2 – Programming 2 3


Generics
• Example: Using ArrayList with Integer data
type

Retrieve:

Spring 2024 61FIT3PR2 – Programming 2 4


Generics
• Example: Using ArrayList with String data type

Retrieve:

Spring 2024 61FIT3PR2 – Programming 2 5


Advantages Generics
• Type checking at compile time
• The Java compiler performs type checking on generic
code to detect issues such as type safety violations.
Fixing errors at compile time is much easier than fixing
errors at runtime.

Spring 2024 61FIT3PR2 – Programming 2 6


Advantages Generics
• No need for type casting
• The following code snippet does not use generics and
requires type casting:

• When using generics, type casting is unnecessary:

Spring 2024 61FIT3PR2 – Programming 2 7


Advantages Generics
• Allows programmers to implement generic
algorithms.
• By using generics, programmers can implement
generic algorithms with various optional data types,
making the code clearer and easier to understand.

Spring 2024 61FIT3PR2 – Programming 2 8


Create Generic Class and Method
• Generics Type Parameter Naming Convention
Character Meaning

E Element

K Key

V Value

T Type

N Number

Spring 2024 61FIT3PR2 – Programming 2 9


Create Generic Class
• Creating a generic class with one type parameter:

Spring 2024 61FIT3PR2 – Programming 2 10


Using Generic Class
• Creating a generic class with one type
parameter:

Spring 2024 61FIT3PR2 – Programming 2 11


Create Generic Class
• Creating a generic class with two type parameter:

Spring 2024 61FIT3PR2 – Programming 2 12


Using Generic Class
• Creating a generic class with two type
parameter:

Spring 2024 61FIT3PR2 – Programming 2 13


Limit data types

Spring 2024 61FIT3PR2 – Programming 2 14


Limit data types

Spring 2024 61FIT3PR2 – Programming 2 15


Generic Placeholder Symbols

Spring 2024 61FIT3PR2 – Programming 2 16


Generic Placeholder Symbols
• Example

Spring 2024 61FIT3PR2 – Programming 2 17


Symbols <?>

Spring 2024 61FIT3PR2 – Programming 2 18


Symbols <? extends type>

Spring 2024 61FIT3PR2 – Programming 2 19


Symbols <? super type>

Spring 2024 61FIT3PR2 – Programming 2 20


Generic Contructors

Spring 2024 61FIT3PR2 – Programming 2 21


Generic Interfaces

Spring 2024 61FIT3PR2 – Programming 2 22


Generic Interfaces

Spring 2024 61FIT3PR2 – Programming 2 23


Some limitations of generics
• Unable to initialize generics with primitive data
types

• Cannot create an instance for data type

Spring 2024 61FIT3PR2 – Programming 2 24


Some limitations of generics
• Cannot be static within a class

class Gen<T>{
static T obj; //The type T cannot be static
static T getObj(){ //Methods cannot be static
return obj;
}
}

Spring 2024 61FIT3PR2 – Programming 2 25


Some limitations of generics
• Cannot create arrays

• Limitations on generic exceptions


• A generic class cannot inherit from the Throwable
class. Therefore, we cannot create a generic
exception class.

Spring 2024 61FIT3PR2 – Programming 2 26


Summary
• Generics Definition
• Advantages Generics
• Create Generic Class and Method
• Limit data types
• Generic Placeholder Symbols
• Generic Contructors
• Generic Interfaces
• Some limitations of generics

Spring 2024 61FIT3PR2 – Programming 2 27

You might also like