Make Selenium Easy

#3. Generic Interface In Java

Introduction

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.

Generic Interface

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.

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.

Implementing Generic Interface

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.

Non generic implementation of generic interface

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.

Generic implementation of generic interface

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

Raw implementation of a generic interface

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

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.

#3. Generic Interface In Java

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