Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Generic Class & Generic Methods

Story :

In the above example we have seen a God was given the responsibility to manage the Animals. Now, let us consider a scenario where a Supreme God was given the responsibility to manage all living beings but only one at a time.

What I mean to say is, if a Cat or a Dog or a Fish is supplied to him, it will be quitetough for him to manage. So he can manage only one object at a time.

class ManageEverything <T> {

  public void manage(List <T> everything){

    System.out.println("Manages in his own way");
  }
}

public class TestManager{
  public static void main(String[] args){

  ManageEverything <Dog> manageEverything = new ManageEverything <Dog>();
  List <Dog> dogs = new ArrayList <Dog>();
  dogs.add(new Dog());
  dogs.add(new Dog());
  manageEverything.manage(dogs);
  List <Fish> fish = new ArrayList <Fish> ();
  fish.add(new Fish());
  manageEverything.manage(fish); // Compiler throws an error.
  }
}


In the above example we have used something called <T>, named as Type.


class ManageEverything <T>

This means the above class can accept any Type(Be it Cat, Dog, Fish) and make sure the same Type is maintened throughout the class.

To make life easier, let's check the below declaration :


ManageEverything <Dog> manageEverything = new ManageEverything <Dog>();


We have declared the class with generic type Dog(e.g. <Dog> ). Which means the <T> in ManageEverything class will be replaced by Dog.


class ManageEverything <Dog>{

  public void manage(List <Dog> everything){

    System.out.println("Manages in his own way");
  }
}

Now the above method manage() can only accept Dog objects. And we we try to insert anything other than Dog, the compiler complains with an error.


List <Fish> fish = new ArrayList <Fish>();
fish.add(new Fish());
manageEverything.manage(fish); // Compiler throws an error.

Generic Methods

In the above case we have made a class as Generic class. i.e. At the class level we did put the Generic Type '<T>'.

But think for a moment, every time we don't need to put the '<T>' in the class level. Sometimes we could wish to put it in the method level as well.

Let's see it below:


class ManageEverything{

  public <T> void manage(List <T> everything){

    System.out.println("Manages in his own way");
  }
}

public class TestManager{
  public static void main(String[] args){

  ManageEverything manageEverything = new ManageEverything();
  List <Dog> dogs = new ArrayList <Dog>();
  dogs.add(new Dog());
  dogs.add(new Dog());
  manageEverything.manage(dogs);
  List <Fish> fish = new ArrayList <Fish>();
  fish.add(new Fish());
  manageEverything.manage(fish); // Compiler throws an error.
  }
}

Notice in the above case we have put the <T> in the method level in between the public keyword and the return type void.


public <T> void manage(List <T> everything)

Also we didn't put any generic type in the class declaration:


ManageEverything manageEverything = new ManageEverything();