Java

What are Generics in Java

While programming in Java, if you want to use multiple classes, functions, and methods in a single program, Java provides the facility to create generic classes or functions with a specific name. More specifically, Generic works in the same way as non-generic and main classes or functions work. However, objects of the normal class need to be called in the main class.

This post will demonstrate the generic and types of generic in Java.

What are Generics in Java?

Java Generics defines the set of the related methods and same data types. Generics permits the multiple data types, including string, integer, all types of parameters, user defined methods, various classes, interfaces and functions. These are mostly utilized by different classes, such as HashSet or HashMap, and so on.

Types of Generic in Java

There are various generic types, and we will discuss some of them:

How to Use Generic Classes in Java?

A generic class has the same implementation as a non-generic class. The classes are referred to as parameterized or parameterized types if they take one or more parameters. The presence of a type parameter section is the sole distinction. There may be different parameter types, each separated by a comma.

To use it, create a generic class with a name. Inside the generic class, make an object. Then, call the created object and utilize “this” keyword along the object. After that, utilize the “getobject()” method to return the created object:

class examp<C> {

  C obj;

  examp(C obj) { this.obj = obj; }

  public C getObject() { return this.obj; }

}

Now, inside the main class, create the object of integer type with a different name:

examp<Integer> iObj = new examp<Integer>(15);

Utilize the println() and pass the created object as the argument to display it on the console:

System.out.println(iObj.getObject());

Now, create string type object and set the value of the defined variable:

examp<String> sObj= new examp<String>("Linuxhint");

Then, print the output on the console by using the “println()” method:

System.out.println(sObj.getObject());

As a result, the created objects have been printed on the console:

How to Use Generic Function in Java?

We can also make a generic function in Java. For this purpose, make a generic function inside the main Java class and pass the parameter to the defined function. Then, utilize the “println()” method and pass the arguments:

static <F> void genericDisplay(F items)

 {

  System.out.println(items.getClass().getName() + " = " + items);

 }

Next, call the function inside the main function with the help of “genericDisplay()” Java method and pass the argument according to defined parameters:

genericDisplay(12);

genericDisplay("Linuxhint");

genericDisplay(1.0);

That’s all about the generics in Java.

Conclusion

Generics defines the set of the related methods and same data types. We can create generic classes, functions, and methods inside a program. To do so, the generic class is created before the main class, and the generic function can be defined before the main function. Then, call the generic class or function in the main class and function consecutively. This post has explained the usage of generics in Java.

About the author

Hafsa Javed