Java Generic Interface Tutorial

In this section, we will learn what the generic interface is and how it works in Java.

Prerequisite: generic classes.

What is Generic Interface in Java? And How to Declare a Generic Interface in Java?

Other than classes, we can turn interfaces into generic. There’s pretty much no difference between a generic class and a generic interface. For both, we use the pair of angle brackets `<>` after the name of that class/interface and of course, set the generic names inside the angle brackets. After that, those generic names can be used inside the body of the interface for the members’ datatype.

How to Implement a Generic Interface?

There are three ways to implement a generic interface:

  1. Applying Generic Interface on a Generic Class
  2. Applying a generic interface on a non-generic class
  3. Using the lambda expressions.

1- Implementing a Generic Interface as if it’s a Non-Generic Interface

The target class can be non-generic class. In this case, the replaced data type for each type parameter should be declared right where the interface implementation happens.

Example: implementing a Generic Interface as Non-Generic Interface

public class Generic implements MyGenericInterface<String,String> {
    @Override
    public void printDetail(String ft, String sd) {
        System.out.println("The value of the ft variable is: "+ft);
        System.out.println("The value of the sd variable is: "+sd);
    }
}

Now let’s use this `Generic` class to create an object from:

public class Simple {

    public static void main(String[] args) {
        MyGenericInterface<String, String> myGenericInterface = new Generic();
        myGenericInterface.printDetail("John","Doe");
    }
}

Output:

The value of the ft variable is: John

The value of the sd variable is: Doe

Note:

  • The replaced data types that we used in the angle brackets before the myGenericInterface variable should match the data type that we’ve set when implementing the interface in the `Generic` class.

2- Implementing a Generic Interface via a Generic Class

The second way to implement a generic interface is to have a generic class with the same type of parameters.

This way at object creation time, when we are replacing the type parameters of the generic class with the real datatype, the type parameters of the target generic interface will be replaced with actual datatype as well.

Example: Implementing a Generic Interface via a Generic Class

public class Generic<T,X> implements MyGenericInterface<T,X> {
    @Override
    public void printDetail(T ft, X sd) {
        System.out.println("The value of the ft variable is: "+ft);
        System.out.println("The value of the sd variable is: "+sd);
    }
}

The `Generic` class has the same number of type parameters as the `MyGenericInterface` has.

Now we can use the `Generic` class to create an object and use the `printDetail` method.

Example:

public class Simple {

    public static void main(String[] args) {
        MyGenericInterface<String, String> myGenericInterface = new Generic<>();
        myGenericInterface.printDetail("John","Doe");
    }
}

Output:

The value of the ft variable is: John

The value of the sd variable is: Doe

Java Generic Interface and Lambda Expression

If the target interface was functional, we can use lambda expression as well. For example, the `MyGenericInterface` you saw at the beginning of this section, is function and so if we wanted to create a lambda expression out of this interface we could go like this:

Example: Generic Interface and Lambda Expression

public class Simple {

    public static void main(String[] args) {
        MyGenericInterface<String, String> myGenericInterface = (a ,b)->{
            System.out.println("The first value is: "+a);
            System.out.println("The second value is: "+b);
        };
        myGenericInterface.printDetail("John","Doe");
    }
}

Output:

The first value is: John

The second value is: Doe

As you can see, we replaced the type parameters with `<String, String>` so now the type of parameters in the `printDetail` method is `String`.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies