Generics in
Java
Agenda
⬡ Generics in java
⬡ Use of generics in java
⬡ Types of generics
⬡ Type parameter
⬡ Bounded type parameter
⬡ Wild cards in java generics
2
WHAT IS GENERICS IN JAVA
Generics specify the class or method for different data types.
They are used mostly for collections where parameter types for
the collection are specified not the base type. using generics it is
possible to create classes that work with different data types.
Uses of generics in java.
1. Provide type safety: This means that it prevents the code from
accessing memory in inappropriate ways by controlling the memory
access of each object.
2. Resolve type casting: type casting is bascally converting one data
type to another. it also means converting a lower type data like like an
int to a higher type data like a double.
*note type casting is not possible for boolean data types
Types of generics
Generic method: Generic methods are methods that introduce
their own type parameters. this is similar to declaring a generic
type but the type parameter’s range is limited to the method
where it is declared. The generic method takes a parameter and
after performing a task. the compiler takes care of the type safety
which enables programmers to code easily since they do not long
individual type castings
Types of generics
Generic class: A generic class simply means that the items or
functions in that class n 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. like in c++ we use “< >” to specify parameter
types in generic class creation.
Type Parameter
Bounded types: parameter type can be bounded by some range
using “extends” keyword. They are used to restrict types that can
be used as type arguments in a parameterized type. for example
a method that operates on numbers might only want to accept
instances of number or its subclasses. in addition to limiting the
types you can use to instantiate a generic type, bounded type
parameters allow you to invoke methods in the bounds.
Type Parameter
The type parameters naming conventions are important to learn
generics thoroughly. the common type parameters are as
follows:
T- Type
E- Element
K- Key
N- Number
V- Value
Wildcards in java generics
in java generics the wildcard(question mark “?”) represents an
unknown type. the wildcard can be used in a variety of situations
such as the type of a parameter, field or local variable;
sometimes as a return type. unlike arrays, different instations of
a generic type are not compatible with each other, not even
explicitly. this incompatibility may be softened by the wildcard.
Wildcards in generics
Upper Bounded Wildcards: they can be used to relax the
restrictions on a variable. To declare an upper-bounded
wildcard character (?), followed by the extends keyword,
followed by its upper bound.
Lower Bounded Wildcards: it is expressed using the wildcard
character (?), followed by the super keyword followed by its
lower bound: <? super A>.
Wildcards in generics
Unbounded Wildcard: This wildcard is specified using the
wildcard character (?), for example, List. this is called a list of
unknown types. they are useful in the following cases:
when writing a method that can be employed using
functionality provided in object class.
when the code is using methods in the generic class that
does not depend on the type parameter
Advantages of generics
Code reuse
Type safety
Individual type casting is not needed
Generics promote code reusability
Implementing generic algorithms