Formal definition of generics in java :

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.
Let us understand generics with respect to the underlined terms in the definition :
1. with a single method declaration, a set of related methods :
Consider following methods :
 public String add(String one,String two){
     return one + two;
}
public int add(int one,int two){
     return one + two;
}
public double add(double one,double two){
   return one + two;
}
By observing this simple scenario we can conclude following,
1. All 3 methods are doing same operation.
2. Each method handles/adds a different data type.
So we can say that, Problem Statement : we are having 3 different methods to do same operation in our code just because we need to handle different data types that leads to redundancy in our code.
Solution with generics :

The same can be achieved with a single generic method which looks as follows :
public <S> S add(S one,S two){
    return one + two;
}
What is ‘S’ in above example ? it is called a TYPE PARAMETER .
It is a placeholder for any type that can be supplied during method call.
In our case we can call it for String , int and boolean as :
String newString = add<String>(“Hello “,”Generics”);
int newInt = add<Integer>(1,5);
double newDouble = add<double>(13.34343,44.44343);

2.  with a single class declaration, a set of related types
     Consider we have following classes :
      class CoalContainer{
          Coal content;
         public void setContent(Coal coal){
                  content = coal;
           }
           public Coal getContent(){
                 return content;
         }
        public int calculateQuantity(){
            // some logic to calculate
       }
       }

 class SugarContainer{
          Sugar content;
         public void setContent(Sugar coal){
                  content = coal;
           }
           public Sugar getContent(){
                 return content;
         }
         public int calculateQuantity(){
            // some logic to calculate
       }
       }

Assume if a container can hold multiple type of goods, Water,Milk,Iron and so on…
Problem Statement :
We would have to write a container to deal with each content type and the methods to operate on it.
Here Java’s concept of Abstract classes in conjunction with genetics helps :

abstract class Container<T> {

T content;// can be sugar , wood, coal and so on…

public void setContent(T value){

content = value;

}

public T getContent(){

return content;

}

//we can handle calculation of quantities as follows.

// create an abstract method for any custom calculation

protected abstract int calculateQuantity();

// provide a default implementation

public int calculateDefaultQuantity(){

//some logic

}

 

}


 

3. TYPE SAFETY :

Generics are very helpful in ensuring type safety in java code

consider following class which validates Views :

 

class Validator<T extends View>{

 

public validate(T view){

//validation code

}

 

}

Above BOUNDED PARAMETER ‘T’ ensures that only the classes

which are Views are allowed to be operated by this class.


 

Leave a Reply

Your email address will not be published. Required fields are marked *