Generics
Contents
Generics enable types (That is, both classes and interfaces) to be parameters of class, interface and method definitions
They are powerful, and benefit in several ways:
- Remove the need for continuous type-casting
- Allow types to be statically checked during compile
- Allow for the implementation of generic algorithms that work on collections of various types
Syntax
While the naming of the type placeholder is arbitrary, there are some commonly used placeholders:
E
- elementK
- keyN
- numberT
- typeV
- valueS
- 2nd typeU
- 3rd TypeV
- 4th Type
There is also the ?
wildcard symbol, which means "any object" - however it does not capture the type
Generic Classes
|
|
Here, we have defined a class which is of variable type T
. When we retrieve the item, that item will be statically returned as having type T
.
(new Container<Circle>(new Circle())).getStoredItem().getClass()
-> Circle
Generic Interfaces
|
|
Similarly, we can apply generics to interfaces like we did to classes
Generic Methods
|
|
Source: Oracle Docs
If a class type is not already defined by a generic class nor a generic interface, types for a generic method can be defined before the return type in the definition.
Bounded Methods
Bounded class methods restrict the class type that can be accepted in a method.
<T extends Integer>
- Type T
that extends Integer
<T super Integer>
- Type T
that Integer
extends.
Multiple Bounds
<T extends B1 & B2 & B3>
- Type T
that extends B1
, B2
, and B3
If one of the bounds is a class, it must be specified first.
Wildcard Methods
<? extends Integer>
- Any object that extends Integer
<? super Integer>
- Any object that Integer
extends.
Unbounded Wildcards
When the needed type of a generic is unknown, the unbounded wildcard can be used.
e.g. List<?>