Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

#1. What is Generics Class in Java?

Posted on 12/11/2024 By admin

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?

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.

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

Uncategorized

Post navigation

Previous Post: protractr
Next Post: how to install testng plugin in eclipse

Related Posts

How To Set The Size Of Browser Window In Selenium? Uncategorized
image – Make Selenium Easy Uncategorized
March 30, 2018 – Make Selenium Easy Uncategorized
April 5, 2017 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Handle Horror Of NullPointerException Using Optional Class Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark