What is "generic"

Generics are a parametric form of polymorphism that allows instantiating classes where one or more of its attributes will have their types defined during the instantiation of this class.

This facility is found in some programming languages such as Java and .NET. In Java they exist since version 5 which was released in 2004.

Generics are especially useful when creating collections of objects, because they have their type defined avoid Casts and excessive checks.

Sample collection without the use of generics:

List lista = new ArrayList();
lista.add(1);           //autobox para Integer
lista.add("dois");
lista.add(new Object());

To List was created without the use of generic, meaning that it is a collection of objects of the type Object, so we can add whatever we want to it (except primitives), this makes it so that every time we want to remove one it comes in the format Object, so one should do the cast for the type you want to use, for example:

Integet i = (Integer)list.get(0);

Sample collection using generics:

List<Integer> lista = new ArrayList<>();
lista.add(1);           

Now, you can only include objects like Integer in the variable lista, this security allows us to take the element without the use of cast, for example:

Integer i = list.get(0);

Creating a generic class

An example of a class that has a polymorphic element defined in its instantiation, i.e., generic:

class MeuGenerico<T> {  //T abreviacao de tipo
    private T var;
    public MeuGenerico() { }
    public MeuGenerico(T var) { this.var = var; }
    public T getVar() { return var; }
    public void setVar(T var) { this.var = var; }
}

Java uses a naming convention for the generic identification letters, (which can be found in several JDK classes, among them are mainly the Collection Framework), and:

E - Element
K - Key
N - Number
T - Type
V - Valor