Generic Interface in Java with Example
Similar to generic class, when an interface is defined using generic type parameter, it is called generic interface in Java.
When a generic interface is implemented by either a generic class or non-generic class, the type argument must be specified.
The generic interface has main two advantages that are as:
- A generic interface can be implemented for different types of data.
- It allows placing constraints (i.e. bounds) on the types of data for which interface can be implemented.
Syntax Declaration of Generic Interface
The general syntax to declare a generic interface in Java is as follows:
interface interface-name<T>
{
void method-name(T t); // public abstract method.
}
In the above syntax, <T> is called a generic type parameter that specifies any data type used in the interface. An interface can also have more than one type parameter separated by a comma.
We have studied already that whenever there is an interface, we should also have implementation classes that implement all methods of an interface.
The general syntax to write an implementation class that implements the above interface, as follows:
class class-name<T> implements interface-name<T>
{
public void method-name(T t)
{
// Write code for this method as per requirement.
}
}
Here, T represents a generic parameter that specifies any data type.
Let’s understand the above syntax with the help of an example.
// A generic interface with one type parameter T.
interface A<T> // Interface name is A.
{
public void m1(T t); // An abstract method with parameter t of type T.
}
// Generic class with one type parameter T implementing generic interface.
class B<T> implements A<T>
{
public T obj;
public void m1(T t)
{
obj = t;
}
}
// Non-generic class implementing generic interface.
class C implements A<Integer>
{
public Integer obj;
public void m1(Integer t)
{
obj = t;
}
}
Generic Interface Example Program
Let’s take a very simple example program where we will create a generic interface A and implementing class Test. Then the implementation class will implement the method of interface A to display a message.
Example 1:
// A generic interface.
public interface A<T>
{
// Declare an abstract method with parameter t of type T.
public void m1(T t);
}
// This generic class implements generic interface.
public class Test<T> implements A<T> {
public void m1(T t)
{
System.out.println("m1-Test");
}
public static void main(String[] args)
{
// Create an object of class Test of type integer.
Test<Integer> t = new Test<Integer>();
t.m1(25); // Calling method.
// Create an object of class Test of string type.
Test<String> t2 = new Test<String>();
t2.m1("John"); // Calling method
}
}
Output: m1-Test m1-Test
Let’s take an example program where we will create a generic interface and implementation class. Then the implementation class will be used to display the class name of object passed to the method.
Example 2:
public interface Fruit<T>
{
// This method can accept any type of object.
public void taste(T fruit);
}
public class AnyFruit<T> implements Fruit<T>
{
public void taste(T fruit)
{
// Get the class name of object passed to this method.
String fruitname = fruit.getClass().getName();
System.out.println(fruitname);
}
}
public class Banana {
}
public class Orange {
}
public class GenericClass {
public static void main(String[] args)
{
// Create an object of class Banana and pass it to class AnyFruit.
Banana b = new Banana();
AnyFruit<Banana> fruit1 = new AnyFruit<Banana>();
fruit1.taste(b);
// Create an object of Orange and pass it to class AnyFruit.
Orange o = new Orange();
AnyFruit<Orange> fruit2 = new AnyFruit<Orange>();
fruit2.taste(o);
}
}
Output: genericsProgram.Banana genericsProgram.Orange
Let’s create a program where we will create a generic interface Myinterface that declares methods max() and min(). Both methods will return the maximum and minimum values from some set of array objects. Look at the source code to understand clearly.
Example 3:
// A generic interface with bounded type parameter.
public interface Myinterface<T extends Comparable<T>>
{
T min();
T max();
}
// A generic class with bounded type parameter.
public class Myclass<T extends Comparable<T>> implements Myinterface<T>
{
T[ ] values;
Myclass(T[] values)
{
this.values = values;
}
public T min()
{
T v = values[0];
for(int i = 0; i < values.length; i++)
{
if(values[i].compareTo(v) < 0)
v = values[i];
}
return v;
}
public T max()
{
T v = values[0];
for(int i = 0; i < values.length; i++)
{
if(values[i].compareTo(v) > 0)
v = values[i];
}
return v;
}
}
public class Test
{
public static void main(String[] args)
{
Integer[ ] inums = {25, 30, 10, 45, 21, 05};
Character[ ] chars = {'a', 'g', 'w', 'i', 'p'};
Myclass<Integer> iobj = new Myclass<Integer>(inums);
Myclass<Character> cobj = new Myclass<Character>(chars);
System.out.println("Max value in inums: " +iobj.max());
System.out.println("Min value in inums: " +iobj.min());
System.out.println("Max value in chars: " +cobj.max());
System.out.println("Min value in chars: " +cobj.min());
}
}
Output: Max value in inums: 45 Min value in inums: 5 Max value in chars: w Min value in chars: a
In this program, a generic interface has been declared whose type parameter is T and its bound type parameter is Comparable, which is an interface defined in java.lang package.
Comparable is also a generic interface after Java 1.5 version that takes a type parameter and specifies the type of objects being compared.
A class that implements Comparable interface defines objects that can be ordered. The generic interface is implemented by Myclass with type parameter T and bounded type parameter Comparable interface.
Then, type parameter T is passed to Myinterface. This is because Myinterface needs a type that implements Comparable, the implementing class (Myclass in this case) must specify the same bound. Once this bound has been set, there is no need to specify it again in the implements clause.
When a non-generic class implements a generic interface, the type parameter does not follow class name. The type parameters must be replaced by actual types while implementing the generic interface.
Let’s take a very simple example program based on it.
Example 4:
public interface Myinterface<T>
{
T m1();
}
// A non-generic class implementing a generic interface.
public class Myclass implements Myinterface<Integer>
{
public Integer m1()
{
System.out.println("m1-Myclass");
return null;
}
public static void main(String[] args)
{
Myclass obj = new Myclass();
obj.m1();
}
}
Output: m1-Myclass
In this tutorial, we have covered almost all the important topics related to generic interface in Java with example programs. We hope that you will have understood the basic concepts of generic interface and practiced all programs. Stay tuned with the next tutorial.
Thanks for reading!!