1.
Generics
a.
Generics in Java is one of important feature added in
Java 5 along with Enum, autoboxing and varargs , to provide compile time
type-safety.
b.
Generic in Java is added to provide compile time
type-safety of code and removing risk of ClassCastException at runtime which
was quite frequent error in Java code,
c.
Generics allows Java programmer to write more robust
and type-safe code.
d.
generics expand your ability to reuse code and let you
do so safely and easily.
2.
Non Generics
Class using Object
a.
This makes NonGen able to store any type of object, as
can the generic version
b.
Its not good because following two reason .
i.
explicit casts must be employed to retrieve the stored
data
ii.
many kinds of type mismatch errors cannot be found
until run time.
3. The General Form of a Generic Class
a.
syntax for declaring a generic class:
i.
class class-name<type-param-list> { // ...
b.
syntax for declaring a reference to a generic class
i.
class-name<type-arg-list> var-name =new
class-name<type-arg-list>(cons-arg-list);
4. bounded types
a.
When specifying a type parameter, you can create an
upper bound that declares the superclass from which all type arguments must be
derived.
b.
This is accomplished through the use of an extends
clause
c.
In addition to using a class type as a bound, you can
also use an interface type. In fact,
d.
you can specify multiple interfaces as bounds.
e.
a bound can include both a class type and one or more
interfaces. In this case, the class type must be specified first.
f.
class Gen<T extends MyClass & MyInterface> {
// ...
g.
Here, T is bounded by a class called MyClass and an
interface called MyInterface. Thus,any type argument passed to T must be a
subclass of MyClass and implement MyInterface.
h.
Syntax
i.
<T extends superclass>
ii.
This specifies that T can only be replaced by
superclass, or subclasses of superclass.
5. Using Wildcard Arguments
a.
The wildcard argument is specified by the ?, and it
represents an unknown type.
b.
One last point: It is important to understand that the
wildcard does not affect what type of Stats objects can be created. This is
governed by the extends clause in the Stats declaration. The wildcard simply
matches any valid Stats object.
6. Bounded Wildcards (Ex. Generic7)
a.
In general, to establish an upper bound for a wildcard,
use the following type of wildcard
b.
expression:<? extends superclass>
c.
where superclass is the name of the class that serves
as the upper bound
d.
You can also specify a lower bound for a wildcard by
adding a super clause to a wildcard
e.
declaration. Here is its general form:<? super
subclass>
f.
In this case, only classes that are superclasses of
subclass are acceptable arguments. This is an exclusive clause, because it will
not match the class specified by subclass.
7. Creating a Generic Method(Ex Generic8)
a.
it is possible to create a generic method that is
enclosed within a non-generic class.
b. generic
methods can be either static or non-static. There is no restriction in this
regard.
c. Generalized
syntax
d. <type-param-list>
ret-type meth-name(param-list)
{ // ...
8. Generic Constructors (Ex Generic9)
a.
It is also possible for constructors to be generic,
even if their class is not.
9. Generic Interfaces
a.
you can also have generic interfaces
b.
In general, a generic interface is declared in the same
way as is a generic class.
c.
In general, if a class implements a generic interface,
then that class must also be generic, at least to the extent that it takes a
type parameter that is passed to the interface. For example,the following
attempt to declare MyClass is in error:
d.
class MyClass implements MinMax<T> { //
Wrong!{ // OK
e.
Because MyClass does not declare a type parameter,
there is no way to pass one to MinMax.
f.
In this case, the identifier T is simply unknown, and
the compiler reports an error. Of course,if a class implements a specific type
of generic interface, such as shown here:
g.
class MyClass implements MinMax<Integer> { //
OKthen the implementing class does not need to be generic.
h.
Benefit of Generic Interface
i.
it allows you to put constraints (that is, bounds) on
the types of data for which the interface can be implemented.
ii.
it can be implemented for different types of data.
i.
Syntax:
i.
interface interface-name<type-param-list>
{ //
ii.
Here, type-param-list is a comma-separated list of type
parameters. When a generic interface is implemented, you must specify the type
arguments, as shown here:
iii.
class class-name<type-param-list> implements
interface-name<type-arg-list> {
10. Raw Types and Legacy Codes
a.
Because support for generics is a recent addition to
Java, it was necessary to provide some transition path from old, pre-generics
code. At the time of this writing, there are still millionsand millions of
lines of pre-generics legacy code that must remain both functional and
compatible with generics. Pre-generics code must be able to work with generics,
and generic code must be able to work with pre-generic code.
b.
To handle the transition to generics, Java allows a
generic class to be used without any type arguments. This creates a raw type
for the class. This raw type is compatible with legacy code, which has no
knowledge of generics. The main drawback to using the raw type is that the type
safety of generics is lost.
c.
Generic11
d.
A raw type is not type safe. Thus, a variable of a raw
type can be assigned a reference to any type of Gen object. The reverse is also
allowed;
e.
Generic Class Hierarchies
11. Rules of Genrics
a.
Parametrized type like Set<T> is subtype of raw
type Set and you can assign Set<T> to Set, following code is legal in
Java:
Set
setOfRawType = new HashSet<String>();
setOfRawType =
new HashSet<Integer>();
b.
Gen<int> strOb = new Gen<int>(53); // Error,
can't use primitive type