Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

#3. Generic Interface In Java

Posted on 02/19/2025 By admin

As part of the Generics in Java series, we will learn below concepts in this post –

  1. Why do we a need generic interface?
  2. What is a generic interface?
  3. How to create a generic interface?
  4. How to implement a generic class of a generic interface?
  5. How to implement a non-generic class of a generic interface?

If you are new to generics and generic class concepts then I will recommend going through the Generic class in Java.

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

Generics in Java can help us in avoiding duplicating classes and run-time class cast exceptions as explained in the previous post. The same reason is applied to the need for generic interfaces as well.

A generic interface can be created in the same way as we create a generic class.

package genericsexamples;

public interface ObjectInfo  {
        
        T getObjectInfo(T val);

}

The name of the interface is ObjectInfo and T is the type parameter. After the interface name, we need to pass a name/parameter of your choice ( T is a standard) inside angle brackets and use the same name wherever you want to use it.

We can implement a generic interface in the same way as we do for the plain interface. We just need to provide type arguments. However, that is optional. I will explain all possible ways in this post.

You can implement a generic interface in a non generic class. You just need to pass type arguments as below. In this case you are restricting class usage with Integer type. If you want to use other data types then it will be problem.

package genericsexamples;

public class ObjectInfoUsage2 implements ObjectInfo {

        @Override
        public Integer getObjectInfo(Integer val) {
                return val;
        }

        public static void main(String[] args) {

                ObjectInfoUsage2 o1 = new ObjectInfoUsage2();
                Integer i1 = o1.getObjectInfo(10);
                System.out.println(i1);
        }
}

We can implement a generic interface in a generic class as below. It will give flexibility of different type arguments in usage.

package genericsexamples;

public class ObjectInfoUsage3  implements ObjectInfo  {

        @Override
        public T getObjectInfo(T val) {
                return val;
        }

        public static void main(String[] args) {
                
                ObjectInfoUsage3 o1 = new ObjectInfoUsage3();
                Integer i1 = o1.getObjectInfo(10);
                System.out.println(i1);
                
                ObjectInfoUsage3 o2 = new ObjectInfoUsage3();
                Double i2 = o2.getObjectInfo(10.5);
                System.out.println(i2);
                
        }
}

We can skip passing type agruments while implementing a generic interface which is not recommended.

package genericsexamples;

public class ObjectInfoUsage implements ObjectInfo {

        @Override
        public Object getObjectInfo(Object val) {
                return val;
        }

        public static void main(String[] args) {
                
                ObjectInfoUsage o1 = new ObjectInfoUsage();
                Integer i1 = (Integer) o1.getObjectInfo(10);
                System.out.println(i1);
        }       
}

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: Postman Tutorial Part 6 – Understand HTTP Headers in API
Next Post: REST Assured Tutorial 24 – Creating JSON Array Request Body Using List

Related Posts

image – Make Selenium Easy Uncategorized
TestNG Tutorials 49: Need of DataProvider Method in TestNG Uncategorized
April 9, 2019 – Make Selenium Easy Uncategorized
How PageFactory Could Help to Handle StaleElementReferenceException Uncategorized
How To Use Singleton Class to Manage Instance Variables in Automation Framework – Java Uncategorized
Using Thread.sleep() in Selenium WebDriver 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