Make Selenium Easy

#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 –

We will call the above class as below –

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 –

You can use the above class as below –

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.

Let’s use the above class as below –

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-

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 –

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 –

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 <>.

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

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

#1. What is Generics Class in Java?

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: