0
0
JavaHow-ToBeginner · 3 min read

How to Create Generic Interface in Java: Syntax and Example

To create a generic interface in Java, declare the interface with a type parameter inside angle brackets, like interface MyInterface. This allows the interface to work with different data types specified when implemented or used.
📐

Syntax

A generic interface in Java is declared by adding a type parameter in angle brackets after the interface name. This type parameter acts as a placeholder for the actual type that will be specified later.

  • interface: keyword to declare an interface
  • Name<T>: interface name with generic type parameter T
  • T: type parameter used inside the interface methods
java
public interface MyInterface<T> {
    void performAction(T item);
}
💻

Example

This example shows a generic interface Container with a type parameter T. The interface has methods to add and get an item of type T. The class Box implements this interface for String type.

java
public interface Container<T> {
    void add(T item);
    T get();
}

public class Box implements Container<String> {
    private String item;

    @Override
    public void add(String item) {
        this.item = item;
    }

    @Override
    public String get() {
        return item;
    }

    public static void main(String[] args) {
        Box box = new Box();
        box.add("Hello World");
        System.out.println(box.get());
    }
}
Output
Hello World
⚠️

Common Pitfalls

Common mistakes when creating generic interfaces include:

  • Not specifying the type parameter when implementing the interface, which causes raw type warnings.
  • Using different type parameters inconsistently in the interface and implementation.
  • Trying to create instances of the generic type parameter directly (e.g., new T()), which is not allowed.

Always specify the type when implementing and avoid raw types.

java
/* Wrong: raw type usage causes warnings */
public class RawBox implements Container {
    private Object item;

    @Override
    public void add(Object item) {
        this.item = item;
    }

    @Override
    public Object get() {
        return item;
    }
}

/* Right: specify type parameter */
public class TypedBox implements Container<Integer> {
    private Integer item;

    @Override
    public void add(Integer item) {
        this.item = item;
    }

    @Override
    public Integer get() {
        return item;
    }
}
📊

Quick Reference

  • Declare generic interface: interface InterfaceName<T>
  • Use type parameter T in method signatures
  • Specify actual type when implementing: class MyClass implements InterfaceName<String>
  • Do not use raw types to avoid warnings
  • Cannot instantiate type parameter directly (no new T())

Key Takeaways

Declare generic interfaces with a type parameter in angle brackets after the interface name.
Use the type parameter inside interface methods to make them flexible for any data type.
Always specify the actual type when implementing the generic interface to avoid raw type warnings.
Avoid trying to create instances of the generic type parameter directly inside the interface or implementation.
Generic interfaces help write reusable and type-safe code by working with any specified type.