Programming in Java
Topic: Generics
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
Introduction
Benefits of Generics
Generic Classes and Interfaces
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
Enables to create classes, interfaces, and methods in which
the type of data upon which they operate is specified as a
parameter.
Introduced in Java by jdk 1.5.
Generics means parameterized types.
Generics add the type-safety.
Generics add stability to your code by making more of
your bugs detectable at compile time.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why Generics?
The functionality of Gen class can be achieved without
generics by specifying Object type and using proper
casting whenever required.
Then why we use Generics?
Java compiler does not have knowledge about the type of
data actually stored in NonGen. So-
Explicit casts must be employed to retrieve the stored data.
Several type mismatch errors cannot be found until run time.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why Generics?
Stronger type checks at compile time
Elimination of casts
List list = new ArrayList(); list.add("hello");
String s = (String) list.get(0);
Using generics:
List<String> list = new ArrayList<String>(); list.add("hello");
String s = list.get(0); // no cast
Enabling programmers to implement generic algorithms.
We can implement generic algorithms that work on collections of
different types, can be customized, and are type safe and easier to
read.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Advantage of Generics
The ability to create type-safe code in which type-
mismatch errors are caught at compile time is a key
advantage of generics.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Generics Work Only with Objects
When declaring an instance of a generic type, the type
argument passed to the type parameter must be a class
type.
Gen<int> strOb = new Gen<int>(53);
The above declaration is an error.
A reference of one specific version of a generic type is not
type compatible with another version of the same generic
type.
iOb = strOb; // Wrong!
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Generic Class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
General Form of Generic Class
The generics syntax for declaring a generic class:
class class-name<type-param-list>
{ // ... }
The syntax for declaring a reference to a generic class:
class-name<type-arg-list> var-name =
new class-name<type-arg-list>(cons-arg-list);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Generic Class with Multiple Type Parameters
class TwoGen<T, V> {
T ob1; V ob2;
TwoGen(T o1, V o2) {
ob1 = o1; ob2 = o2; }
void showTypes() {
System.out.println("Type of T is " +
ob1.getClass().getName());
System.out.println("Type of V is " +
ob2.getClass().getName()); }
T getob1() { return ob1; }
V getob2() { return ob2; }
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class SimpGen {
public static void main(String args[]) {
TwoGen<Integer, String> t =
new TwoGen<Integer, String>(16920, "Ravi Kant");
t.showTypes();
int v = t.getob1();
System.out.println("value: " + v);
String str = t.getob2();
System.out.println("value: " + str);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bounded Types
Used to limit the types that can be passed to a type parameter.
When specifying a type parameter, we can create an upper
bound that declares the super-class from which all type
arguments must be derived.
<T extends superclass>
A bound can include both a class type and one or more
interfaces.
class Gen<T extends MyClass & MyInterface>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Generic Interfaces
Generic interfaces are specified just like generic classes.
Example:
interface MinMax<T extends Comparable<T>>
{ T min(); T max(); }
The implementing class must specify the same bound.
Once the bound has been established, it need not to be
specified again in the implements clause.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Generic Interface
interface MinMax<T extends Comparable<T>>
{
T min();
T max();
}
class My<T extends Comparable<T>> implements MinMax<T>
{
…
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)