Java Generics

October 21, 2018 Off By ravi24kant

Java Generics Introduction

If I tell you, I can create a method of addition which can add variables of the data type of every method. I mean by every data type, if I want to add integers and add two to the same method double. Will you ask, is it possible
to do this? Yes, it is absolutely possible. This method can be easily made. And not everyone can only make in.
With the help of Java generics, you can create a method that can execute with all types of data types. In these methods, you pass the data type as parameters.

This lets the compiler know what kind of data type to execute with the method.This feature of Java enhances readability, saves time for programmers and saves memory of computer. Because you do not need to type code separately for all types of data types.

Generics can only be used with reference types (class). You can not use generics with any primitive types such as int and char etc. Therefore, all the primitive types in java have been implemented in the form of class. Like for integer and double double etc. Not only the methods, you can also create classes, interfaces and constructors
as generic.

The Structure of of Generic class as given below:

class GenWorld <T>{

}

When you create the object of the above class will look like:

GenWorld<Integer> obj1 = new GenWorld<Integer>(20);

Example of Java Generics

class GenWorld <T>

{

T getValue;

public GenWorld(T n)

{

this.getValue=n;

}

public void display()

{

System.out.println(this.getValue);

}

}

public class GenDemo
{

public static void main(String args[])

{

GenWorld<Integer> intgen = new GenWorld<Integer>(10);

GenWorld<String> Stringgen = new GenWorld<String> (“ravi”);

intgen.display();

Stringgen.display();

}

}

In the example above, as you can see, class is declared differently. Here we have declared generic type in the brackets (<>) before the name of the class . It looks like our class is generic.

Now you see the variable declaration, here is the generic type of our variable on which we created when we created the above class. After this, a constructor declaration, in which the generic argument is passing. In fact, when you create an object , the type of data you enter will be converted to it. This is the same if you insert an integer, it will become an integer and you will enter the string then the string.

Now it is the most important thing to come here, as you can see that the genericObjects of class are created in slightly different ways. The kind of data
you are going to pass in the argument, you have to tell when creating an
object. As I have done in the example above.

Generic Methods

As you saw in the example above, the generic type of a generic class can be used, so that method is also generic. But if you want to declare a generic  method in a class that is not itself a generic one. Generic methods for creating structures are being given below.

void myMethod (T x)

{

T a = x;

System.out.println (“hello”, a);

}

Generic Constructor

It is possible for constructors to be generic, even if their class is not. For example, in the following program, DisplayValue constructor specifies a parameter of a generic type, even though DisplayValue is not a generic class, its constructor is generic.

class DisplayValue {

<T> DisplayValue(T ob)

{

System.out.println(“T Value :”+ obj);

}

}

public class Myapp {

public static void main(String[] args)

{

DisplayValue dv1 = new DisplayValue(60);

DisplayValue dv2 = new DisplayValue(“Fiftyone”);

}

}

Program Output

T Value : 50

T Value : Fiftyone

Generic Interfaces

In Similar way, we created the Generic Classes, Methods and constructors, we can also declare  generic interfaces. it is very simple as

public interface myInterface

{

public T myFunction();

}

public class myClass implements myInterface

{

public T myFunction()

{

//

}

}

Bounded Types

In the above example, you can replace T with any data type. It is okay to do
this, but it can be done whenever the user only passes numbers. Meaning you
want the user to stop passing any data type other than numbers.
Provides java bounded types to handle such a situation. When declaring Type
parameter (T), you extend the super class of the type you want to use. By
doing so, T can be replaced by just the type of super class. Its structure is
being given below –
class GenWorld <T extends Number>
{
// In this class you can use only numbers instead of T.
}