Generic Interfaces

Parameterized interfaces

4 min read

Generic Interfaces

Interfaces can also be parameterized with generic types.

Code Examples

Generic interface implementation

java
1
2interface Repository<T> {
3    void save(T t);
4}
5
6class UserRepository implements Repository<User> {
7    public void save(User user) {}
8}
9          

Use Cases

  • DAO pattern
  • Framework abstractions

Common Mistakes to Avoid

  • Using raw interface types