COMP2511
Generics in Java
(Part 2)
Prepared by
Dr. Ashesh Mahidadia
Generics in Java: Java Tutorial
v Good introduction at the following web page, Oracle’s official Java Tutorial,
you must read all the relevant pages!
https://docs.oracle.com/javase/tutorial/java/generics/index.html
v The following lecture slides cover only some parts of the above tutorial,
however, you should read all the relevant sections (pages) in the above tutorial.
COMP2511: Generics and Collections in Java 2
https://docs.oracle.com/javase/tutorial/java/generics/index.html
Generics in Java (Recap)
Generics enable types (classes and interfaces) to be parameters when defining:
• classes,
• interfaces and
• methods.
Benefits
v Removes casting and offers stronger type checks at compile time.
v Allows implementations of generic algorithms, that work on collections of different types, can
be customized, and are type safe.
v Adds stability to your code by making more of your bugs detectable at compile time.
COMP2511: Generics and Collections in Java 3
Without Generics With Generics
Generic Types (Recap)
v A generic type is a generic class or interface that is parameterized over types.
v A generic class is defined with the following format:
class name< T1, T2, ..., Tn > { /* … */ }
v The most commonly used type parameter names are:
v E – Element (used extensively by the Java Collections Framework)
v K – Key
v N – Number
v T – Type
v V – Value
v S,U,V etc. – 2nd, 3rd, 4th types
v For example,
Box
OR
Box
COMP2511: Generics and Collections in Java 4
Multiple Type Parameters (Recap)
v A generic class can have multiple type
parameters.
v For example, the generic OrderedPair class,
which implements the generic Pair interface
COMP2511: Generics and Collections in Java 5
v Usage examples,
Pair
Pair
… …
OrderedPair
OrderedPair
… …
OrderedPair
Generic Methods (Recap)
Generic methods are methods that introduce their own type parameters.
COMP2511: Generics and Collections in Java 6
The complete syntax for invoking this method would be:
Pair
Pair
boolean same = Util.
The type has been explicitly provided, as shown above.
Generally, this can be left out and the compiler will infer the type that is needed:
Pair
Pair
boolean same = Util.compare(p1, p2);
Bounded Type Parameters
v There may be times when you want to restrict the types that can be used as type
arguments in a parameterized type.
v For example, a method that operates on numbers might only want to accept instances
of Number or its subclasses.
COMP2511: Generics and Collections in Java 7
Multiple Bounds
v A type parameter can have multiple bounds:
< T extends B1 & B2 & B3 >
v A type variable with multiple bounds is a subtype of all the types listed in the bound.
v Note that B1, B2, B3, etc. in the above refer to interfaces or a class. There can be at
most one class (single inheritance), and the rest (or all) will be interfaces.
v If one of the bounds is a class, it must be specified first.
COMP2511: Generics and Collections in Java 8
Generic Methods and Bounded Type Parameters
COMP2511: Generics and Collections in Java 9
X – invalid
Valid
Generics, Inheritance, and Subtypes
v Consider the following method:
public void boxTest( Box
v What type of argument does it accept?
v Are you allowed to pass in
Box
v The answer is “no”, because Box
Box
v This is a common misunderstanding when it comes to
programming with generics.
COMP2511: Generics and Collections in Java 10
Generic Classes and Subtyping
v You can subtype a generic class or interface by extending or
implementing it.
v The relationship between the type parameters of one class or interface
and the type parameters of another are determined by the extends and
implements clauses.
v ArrayList
v So ArrayList
which is a subtype of Collection
v So long as you do not vary the type argument,
the subtyping relationship is preserved between the types.
COMP2511: Generics and Collections in Java
11
Wildcards: Upper bounded
v In generic code, the question mark (?), called the wildcard, represents an unknown
type.
v The wildcard can be used in a variety of situations: as the type of a parameter, field, or
local variable; sometimes as a return type.
v The upper bounded wildcard, < ? extends Foo >, where Foo is any type, matches
Foo and any subtype of Foo .
v You can specify an upper bound for a wildcard, or you can specify a lower bound, but
you cannot specify both.
COMP2511: Generics and Collections in Java 12
Wildcards: Unbounded
v The unbounded wildcard type is specified using the wildcard character (?),
for example, List< ? >. This is called a list of unknown type.
COMP2511: Generics and Collections in Java 13
It prints only a list of Object instances;
it cannot print List
List
To write a generic printList
method, use List>
Wildcards: Lower Bounded
v An upper bounded wildcard restricts the unknown type to be a specific type or a
subtype of that type and is represented using the extends keyword.
v A lower bounded wildcard is expressed using the wildcard character (‘?’), following by
the super keyword, followed by its lower bound: < ? super A >.
v To write the method that works on lists of Integer and the super types of Integer, such
as Integer, Number, and Object, you would specify List Super Integer>.
v The term List
COMP2511: Generics and Collections in Java 14
Wildcards and Subtyping
v Although Integer is a subtype of Number,
List
these two types are not related.
v The common parent of
List
List>.
COMP2511: Generics and Collections in Java 15
End
COMP2511: Generics and Collections in Java 16