Java - Generics


Generics improves type safety.  It introduces the construct <>:

ArrayList<Animal> animalList = new ArrayList<Animal>();

List<? extends Animal> list = animalsList;
List<? super Animal> superList = dummy;

The extends keyword in the above example indicates that it would accept 
objects of classes that inherit from the Animal class.

The super keyword in the above example indicates that it would accepts 
objects of classes that the Animal class inherit from.

Generics also help with avoiding having to cast.

The generics facility introduced the concept of type variable. A type variable 
is an unqualified identifier introduced by:

1. Generic class declarations
2. Generic interface declarations
3. Generic method declarations
4. Generic constructor declarations.

public interface List<T> extends Collection<T> {
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License