Generic
Interfaces
In addition to generic
classes and methods, 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.
A generic interface example.
A Min/Max interface.
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 GenIFDemo {
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());
}
}
The output is shown here:
Max value in inums: 8
Min value in inums: 2
Max value in chs: w
Min value in chs: b
Although most aspects of this
program should be easy to understand, a couple of key points need to be made.
First, notice that MinMax is
declared like this:
interface MinMax<T extends
Comparable<T>> {
In general, a generic
interface is declared in the same way as is a generic class. In this case, the
type parameter is T, and its upper
bound is Comparable. As explained
earlier, Comparable is an interface
defined by java.lang that specifies
how objects are compared. Its type parameter specifies the type of the objects
being compared.
Next, MinMax is implemented by MyClass.
Notice the declaration of MyClass,
shown here:
class MyClass<T extends
Comparable<T>> implements MinMax<T> {
Pay special attention to the
way that the type parameter T is
declared by MyClass and then passed
to MinMax. Because MinMax requires a type that implements Comparable, the implementing class (MyClass 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, this line is
incorrect and won’t compile:
// This is wrong!
class MyClass<T extends
Comparable<T>>
implements MinMax<T extends
Comparable<T>> {
Once the type parameter has
been established, it is simply passed to the interface without further
modification.
In general, if a class
implements a generic interface, then that class must also be generic, at least
to the extent that it takes a type parameter that is passed to the interface.
For example, the following attempt to declare MyClass is in error:
class MyClass implements MinMax<T> { //
Wrong!
Because MyClass does not declare a type parameter, there is no way to pass
one to MinMax. In this case, the
identifier T is simply unknown, and
the compiler reports an error. Of course, if a class implements a specific type of generic interface, such
as shown here:
class MyClass implements MinMax<Integer>
{ // OK
then the implementing class
does not need to be generic.
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 MinMax example, only types that implement the Comparable interface can be passed to T.
Here is the generalized
syntax for a generic interface:
interface interface-name<type-param-list> { // …
Here, type-param-list is a comma-separated list of type parameters. When
a generic interface is implemented, you must specify the type arguments, as
shown here:
class class-name<type-param-list>
implements interface-name<type-arg-list> {
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.