Sub topic 6
Generic Interfaces
An interface is a list of methods that a class promises to provide. A generic interface is an interface with one or more type parameters. A type parameter is a placeholder for a type, such as T. It works the same way it does in a generic class. The type gets filled in later, when someone uses the interface.
You already use generic interfaces. List<E> is one. Comparable<T> is another. In this chapter you will write your own.
Declaring a Generic Interface
You put the type parameters in angle brackets after the interface name. Then you can use them in the method signatures.
interface Converter<A, B> {
B convert(A input);
}This interface says: "Give me an A, and I will give you back a B." It does not say what A and B are. The class that implements it decides.
Two Ways to Implement It
A class can implement a generic interface in two ways.
- Fix the types. The class picks real types, such as
StringandInteger. The class itself is not generic. - Pass the type parameter along. The class declares its own type parameter and hands it to the interface. The class stays generic.
This complete program shows both styles. Save it as ConverterDemo.java and run it.
interface Converter<A, B> {
B convert(A input);
}
// Style 1: fix the types
class LengthConverter implements Converter<String, Integer> {
@Override
public Integer convert(String input) {
return input.length();
}
}
// Style 2: stay generic
class SameConverter<T> implements Converter<T, T> {
@Override
public T convert(T input) {
return input;
}
}
public class ConverterDemo {
public static void main(String[] args) {
Converter<String, Integer> length = new LengthConverter();
System.out.println(length.convert("generics")); // 8
Converter<String, String> same = new SameConverter<>();
System.out.println(same.convert("hello")); // hello
Converter<Integer, Integer> doubler = n -> n * 2;
System.out.println(doubler.convert(21)); // 42
}
}In LengthConverter, the method takes a String and returns an Integer. The compiler checks that these types match Converter<String, Integer>. If you wrote convert(Object input) instead, the code would not compile.
The last example uses a lambda. A lambda is a short way to write a class that has one method. Converter has only one method, so a lambda can implement it.
Common Mistake
Don't forget the type arguments after the interface name:
class BadConverter implements Converter { // raw type: A and B are lost
public Object convert(Object input) {
return input;
}
}This compiles, but it gives up all type checking. A later chapter covers raw types like this one.
Exercise
Write a class UpperCaser that implements Converter<String, String> and returns the input in upper case. What does new UpperCaser().convert("java") return?
Show answer
class UpperCaser implements Converter<String, String> { @Override public String convert(String input) { return input.toUpperCase(); } } — The call returns "JAVA".
Exercise
Is SameConverter<T> a generic class? Why or why not?
Show answer
Yes. It declares its own type parameter T and passes it to Converter as both A and B, so the caller chooses T, for example SameConverter<String>.