#1. What is Generics Class in Java?

Introduction

Generics were introduced in Java 1.5 with the purpose of identifying probable run time errors related to type at compile time itself. It provides stronger type checks at compile-time and makes a class, interface and methods type-safe. It also eliminates explicit type casting.

You will be aware of the below concepts at end of this tutorial –

  1. Why do we need to use generics?
  2. What is a generic class?

Why Generics?

Suppose you have a requirement to create a class which can store an integer value and return it on call. We will create a class as below –

package genericsexamples;

public class StoreIntergerValue {
	
	private Integer value;
	
	public StoreIntergerValue(Integer value) {
		this.value = value;
	}
	
	public Integer getValue()
	{
		return value;
	}

}

We will call the above class as below –

package genericsexamples;

public class StoreValue {

	public static void main(String[] args) {

		StoreIntergerValue siv = new StoreIntergerValue(10);
		Integer value = siv.getValue();
		System.out.println(value);

	}

}

The output of the above class will be 10.

After some days you got a new requirement to create a class which can store a double value and return it on call. As we already have a class for integers, we just duplicated that as below –

package genericsexamples;

public class StoreDoubleValue {

	private Double value;

	public StoreDoubleValue(Double value) {
		this.value = value;
	}

	public Double getValue() {
		return value;
	}

}
 

You can use the above class as below –

package genericsexamples;

public class StoreValue {

	public static void main(String[] args) {

		StoreIntergerValue siv = new StoreIntergerValue(10);
		Integer value = siv.getValue();
		System.out.println(value);

		StoreDoubleValue siv1 = new StoreDoubleValue(10.5);
		Double value1 = siv1.getValue();
		System.out.println(value1);
	}

}

The output of the above class will be 10 and 10.5.

Again your requirement is enhanced and you need a class which can hold a string value and return it on call. Now you can not keep duplicating classes. Let’s think of something so that we can avoid duplicate classes. We know that the Object class is a superclass of all the classes. So instead of using explicit types Integer, String, Double or user-defined types let’s use Object type.

package genericsexamples;

public class StoreAnyValue {

	private Object value;

	public StoreAnyValue(Object value) {
		this.value = value;
	}

	public Object getValue() {
		return value;
	}
}

Let’s use the above class as below –

package genericsexamples;

public class StoreValue {

	public static void main(String[] args) {

		StoreAnyValue sav = new StoreAnyValue(11.5);
		Double val1 = (Double) sav.getValue();
		System.out.println(val1);
		
		StoreAnyValue sav1 = new StoreAnyValue(10);
		Integer val2 = (Integer) sav1.getValue();
		System.out.println(val2);
	}

}

We need to explicit cast to Integer or Double from Object type to store it in appropriate type variables. We need to know the type in advance to perform casting. Suppose if you do it wrong then you will get ClassCastException. See the example below-

package genericsexamples;

public class StoreValue {

	public static void main(String[] args) {
		// Integer to double casting  - incorrect type
		StoreAnyValue sav1 = new StoreAnyValue(10);
		Double val2 = (Double) sav1.getValue();
		System.out.println(val2);
	}

}

Output

So using Object type solves the problem of duplicate classes but it made class type unsafe at run time. No compile-time error will be thrown on the incorrect type casting. Here comes the concept of Generics in Java which solves both problems.

What is Generics in Java?

Generic enables types to be parameterized over types. We create methods or constructors with parameters and it provides flexibility to use them will different arguments. The same we can use at the class or interface level. It provides a stronger type check at compile time, eliminates explicit type casting and is helpful in developing generic algorithms.

Above classes StoreIntergerValue, StoreDoubleValue and StoreAnyValue can be removed and create only one generic class as below –

package genericsexamples;

public class StoreAnyValueWithGenerics {

	private T value;

	public StoreAnyValueWithGenerics(T value) {
		this.value = value;
	}

	public T getValue() {
		return value;
	}

}

After the class name, we need to pass a name/parameter of your choice ( T is a standard) inside <> and use the same name wherever you want to use it. If you pass Integer as T then the above class will be compiled similar to StoreIntergerValue. If you pass Double as T then the above class will be compiled similar to StoreDoubleValue. This is called generic. It gives you the flexibility to use it with any type.

We need to pass the type parameter while creating a class as below –

StoreAnyValueWithGenerics s1 = new StoreAnyValueWithGenerics<>(10);
StoreAnyValueWithGenerics s2 = new StoreAnyValueWithGenerics<>(10.5);

The generics were introduced in 1.5 but if you are using JDK 1.7 and above then you are not required to pass the type parameter at both diamonds i.e <>.

package genericsexamples;

public class StoreValue {

	public static void main(String[] args) {
		
		StoreAnyValueWithGenerics s1 = new StoreAnyValueWithGenerics<>(10);
		Integer v1 = s1.getValue();
		System.out.println(v1);

		StoreAnyValueWithGenerics s2 = new StoreAnyValueWithGenerics<>(10.5);
		Double v2 = s2.getValue();
		System.out.println(v2);
	}

}

So with one generic class only we are able to store any type of value. If you do incorrect type casting then it will show you an error at compile time itself as below –

That’s all in this post. We are going to learn a lot about generics in upcoming posts.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Leave a Reply

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