Generics and Interface

Generic Interfaces
Generics can also be used with interfaces. Generic interfaces are specified similar to generic classes.

Syntax for creating and implementing generic interfaces:
     interface interface-name <type-param-list>
     {
//…
     }
     class class-name<type-param-list> implements interface-name<type-arg-list>
     {
     //…
     }

The generic interface offers two benefits.
  • First, it can be implemented for different types of data.
  • Second, it allows users to put constraints (that is, bounds) on the types of data for which the interface can be implemented.

// Example – Generic programming and Interface
interface Arithmetic<T1 extends Number>
{
     public void fnAdd(T1 v1, T1 v2);
     public void fnSub(T1 v1, T1 v2);
}
class Gen<T1 extends Number> implements Arithmetic<T1>
{
     double sum, diff;
     public void fnAdd(T1 v1, T1 v2)
     {
          sum = v1.doubleValue() + v2.doubleValue();
     }
     public void fnSub(T1 v1, T1 v2)
     {
          diff = v1.doubleValue() – v2.doubleValue();
     }
     public void fnDisplay()
     {
          System.out.println(“Sum = “+sum);
          System.out.println(“Difference = “+diff);
     }
}
public class GenPgm7
{
     public static void main(String as[])
     {
          Gen<Integer> g1 = new Gen<Integer>();
          g1.fnAdd(4,3);
          g1.fnSub(5,7);
          g1.fnDisplay();
          Gen<Double> g2 = new Gen<Double>();
          g2.fnAdd(4.2,3.5);
          g2.fnSub(9.5,7.6);
          g2.fnDisplay();
     }
}

// Example – Generic programming and Interface
// Minimum and Maximum element in an array
interface MinMax<T1 extends Comparable<T1>>
{
     public void fnMin();
     public void fnMax();
}
class Gen<T1 extends Comparable<T1>> implements MinMax<T1>
{
     T1 min, max;
     T1 [] Arr;
     Gen(T1 [] A)
     {
          Arr = A;
     }
     public void fnMin()
     {
          min = Arr[0];
          for(int i = 1;i<Arr.length;i++)
              if(Arr[i].compareTo(min) < 0 )
                   min = Arr[i];
     }
     public void fnMax()
     {
          max = Arr[0];
          for(int i = 1;i<Arr.length;i++)
              if(Arr[i].compareTo(max) > 0 )
                   max = Arr[i];
     }
     public void fnDisplay()
     {
          System.out.print(“\n\nArray elements : “);
          for(int i = 0;i<Arr.length;i++)
              System.out.print(Arr[i]+”\t”);
          System.out.print(“\nMinimum element = “+min);
          System.out.print(“\nMaximum element = “+max);
     }
}
public class GenPgm8
{
     public static void main(String as[])
     {
          Integer [] A1 = {2,4,1,6,5,3};
          Double []A2 = {2.3, 5.6, 1.3, 4.3};
          String []A3 = {“CSE”,”IT”,”CIVIL”,”EEE”,”AUTO”};
          Gen<Integer> g1 = new Gen<Integer>(A1);
          g1.fnMin();
          g1.fnMax();
          g1.fnDisplay();
          Gen<Double> g2 = new Gen<Double>(A2);
          g2.fnMin();
          g2.fnMax();
          g2.fnDisplay();
          Gen<String> g3 = new Gen<String>(A3);
          g3.fnMin();
          g3.fnMax();
          g3.fnDisplay();
     }
}