Generic Interfaces
Generics also work with interfaces. Thus, you can also have generic interfaces. Generic interfaces are specified just like generic classes. For example :
The MyInterface is a generic interface that declares the method called myMethod( ). In general, a generic interface is declared in the same way as is a generic class. In this case, the type parameter is T. Next, MyInterface is implemented by MyClass. Myclass is a non generic class. Of course, if a class implements a specific type of generic interface, then the implementing class does not need to be generic.
Another Example
The Mathematics is a generic interface that declares the method called powerOf( ). In this case, the type parameter is T, and its upper bound is Number. The Number is a superclass of all numeric classes, such as Integer, Float and Double. Next, Mathematics is implemented by PowerOfThree. Notice the declaration of PowerOfThree, the type parameter T is declared by PowerOfThree and then passed to Mathematics. T is bounded by Number. Because Mathematics requires a type that extends Number, the implementing class ( PowerOfThree in this case) must specify the same bound.
Furthermore, once this bound has been established, there is no need to specify it again in the implements clause. In fact, it would be wrong to do so. For example, following line is incorrect and won’t compile:
Once the type parameter has been established, it is simply passed to the interface without further modification. The generic interface offers two benefits. First, it can be implemented for different types of data. Second, it allows you to put constraints (that is, bounds) on the types of data for which the interface can be implemented. In the Mathematics example, only types that extends the Number class can be passed to T.
Program
interface Mathematics<T extends Number> { int powerOf(T t); } class PowerOfThree<T extends Number> implements Mathematics<T>{ public int powerOf(T i){ return i.intValue()*i.intValue()*i.intValue(); } } public class Javaapp { public static void main(String[] args) { PowerOfThree<Float> pthree = new PowerOfThree<Float>(); System.out.println("5^3 -> "+pthree.powerOf(5f)); } }