Introduction
As part of the Generics in Java series, we will learn below concepts in this post –
- Why do we a need generic interface?
- What is a generic interface?
- How to create a generic interface?
- How to implement a generic class of a generic interface?
- 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.
package genericsexamples; public interface ObjectInfo <T> { 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.
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.
package genericsexamples; public class ObjectInfoUsage2 implements ObjectInfo<Integer> { @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); } }
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.
package genericsexamples; public class ObjectInfoUsage3 <T> implements ObjectInfo <T> { @Override public T getObjectInfo(T val) { return val; } public static void main(String[] args) { ObjectInfoUsage3<Integer> o1 = new ObjectInfoUsage3<>(); Integer i1 = o1.getObjectInfo(10); System.out.println(i1); ObjectInfoUsage3<Double> o2 = new ObjectInfoUsage3<>(); Double i2 = o2.getObjectInfo(10.5); System.out.println(i2); } }
Raw implementation of a generic interface
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