you can also have generic interfaces. Generic interfaces are specified just like generic classes. Here is an example. It creates an interface
called MinMax that declares the methods min( ) and max( ), which are expected to return the minimum and maximum value of some set of objects.
Example
interface MinMax<T extends Comparable<T>> { T min(); T max(); } // Now, implement MinMax class MyClass<T extends Comparable<T>> implements MinMax<T> { T[] vals; MyClass(T[] o) { vals = o; } // Return the minimum value in vals. public T min() { T v = vals[0]; for (int i = 1; i < vals.length; i++) if (vals[i].compareTo(v) < 0) v = vals[i]; return v; } // Return the maximum value in vals. public T max() { T v = vals[0]; for (int i = 1; i < vals.length; i++) if (vals[i].compareTo(v) > 0) v = vals[i]; return v; } } class GenericInterfaces { public static void main(String args[]) { Integer inums[] = { 3, 6, 2, 8, 6 }; Character chs[] = { 'b', 'r', 'p', 'w' }; MyClass<Integer> iob = new MyClass<Integer>(inums); MyClass<Character> cob = new MyClass<Character>(chs); System.out.println("Max value in inums: " + iob.max()); System.out.println("Min value in inums: " + iob.min()); System.out.println("Max value in chs: " + cob.max()); System.out.println("Min value in chs: " + cob.min()); } }
Output
Inside Base Class Inside Derived Class