Java Generic interface

In this chapter you will learn:

  1. How to declare generic interface and implement generic interface
  2. Syntax for Java generic interface
  3. Note for Java Generic interface
  4. Example - Java Generic interface


In Java we create generic interface.


Here is the generalized syntax for a generic interface:

interface interface-name<type-param-list> { // ...

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> {


In general, if a class implements a generic interface, then that class must also be generic. 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.

Generic interfaces are specified like generic classes.


interface MinMax<T extends Comparable<T>> {
  T max();/*  w  w w  .java2 s .  co m*/
class MyClass<T extends Comparable<T>> implements MinMax<T> {
  T[] vals;
  MyClass(T[] o) {
    vals = o;
  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;

public class Main {
  public static void main(String args[]) {
    Integer inums[] = { 3, 6, 2, 8, 6 };
    Character chs[] = { 'b', 'r', 'p', 'w' };
    MyClass<Integer> a = new MyClass<Integer>(inums);
    MyClass<Character> b = new MyClass<Character>(chs);

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use generic class in a non-generic way
  2. Example - Java Raw Types
Home »
  Java Tutorial »
    Java Langauge »
      Java Generics
Java Generic Type
Java Generic Bounded Types
Java Generic Method
Java Generic Constructors
Java Generic Parameter Wildcard
Java Generic interface
Java Raw Types and Legacy Code
Java Generic Class Hierarchies
Java generic types cast
Java Generic Restrictions