Generic Classes In Java

Adwait Kelkar
4 min readMay 25, 2021

Consider the following scenario: you need to generate a list of all living organisms in a given area; it doesn’t matter if they’re humans, animals, or plants; all that matters is that they’re alive. In this situation, you can group them all as living creatures rather than categorize them; similarly, when storing data, the content is more important than the data type and that’s where you use Generics.

What is Generics in Java?

Generics is a term used in Java to describe a set of language features relating to the definition and use of generic methods. Before generics, we used collections to store any form of object, whether it was a generic or non-generic object. Now, generics force the programmer to store a particular type of object.

Now, generics restrict the Java programmer to store only particular sorts of objects.

Why Java Generics?

If you examine the Java collection framework classes, you will notice that the majority of them accept parameters/arguments of type Object. In this form, they can literally take any Java type as an argument and return the same object or argument. They are essentially heterogeneous, that is, they are not of the same type.

The data type of the input in a Java application is not always fixed. An integer, a float, or a Java string can be used as the input. Prior tests were required in order to assign the input to the appropriate data type variable. The data type of the input was checked after it was taken in the old way, and then it was assigned to the appropriate data type variable.

The length of the code and the time it took to execute increased when this approach was implemented. Generics were created to avoid this. When you are using Generics, the parameters in your code are automatically validated at compile time, and the data type is assigned by default. This is where the feature of generics comes in handy.

Types of Java Generics

Generics can be used in Java in 4 ways, as follows:

1. Generic Type Class

2. Generic Interface

3. Generic Method

4. Generic Constructor

But in this blog we will be particularly learning more about Generic Type Class

Generic Type Class

If a class declares one or even more type variables, it is considered to be generic. The type parameters of the Java Class are these variable types. Let’s look at an example to help us comprehend. In the example below, we build a class only with one property, x, whose type is an object.

When you initialize a class with a particular type, the class should only be used with this same type. For example, if one instance of the class is to carry the value x of type ‘String,’ the developer must set and receive the only String type. There is no option to impose this restriction because the property type is Object.

Even though all Java types are subtypes of the Object class, a programmer can set any object and expect any return value type from get ( ).

We can use generics to enforce this type of restriction, as shown below:

You can now safely conclude that the class will not be misused with incorrect types. A simple example of “Genericclass” looks like as shown below:

This is another example which illustrates how we can define a generic class:

public class Box<T> {
private T t;

public void add(T t) {
this.t = t;
}

public T get() {
return t;
}

public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();

integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));

System.out.printf("Integer Value :%d\n\n", integerBox.get());
System.out.printf("String Value :%s\n", stringBox.get());
}
}

This will produce the following result −

Output:-

Integer Value :10
String Value :Hello World

Advantages of Generics in Java

1. Reusability of Code

You can create a strategy, a class, or an interface once and then use it for any type or in any way you need.

2. Individual Types Casting Isn’t Necessary

Basically, every time you recover information from an Array List, you must typecast it. Each recovery task is a major headache due to typecasting. Generics were introduced to eliminate that approach.

3. Implementing a non-generic algorithm

It can compute algorithms that work on a variety of items and are also type safe.

Written By:

Archit Jain

Adwait Kelkar

Raj Gharate

Shraddha Bharambe

--

--