Skip to main content

Generic Interface Java

Java generic interface

  • Generic interfaces in Java a are interfaces that declare one or more type parameters.

  • These parameters allow the interface to operate on objects of various types while providing compile-time type safety.

  • Generic interfaces enable you to create more flexible and reusable code.

Defining a Generic Interface

  • To define a generic interface, you use a type parameter in angle brackets (<>) after the interface name.

  • This type parameter can be used in method signatures within the interface.

Syntax

interface InterfaceName<T> {
// Method signatures that use type parameter T
}
  • InterfaceName is the name of your interface.

  • T is the type parameter that can be used in method signatures.

Example of a Generic Interface

Let's create a generic interface called Storage that has methods to store and retrieve elements of type T.

interface Storage<T> {
void store(T item);
T retrieve();
}

Next, let's implement this interface in a class called SimpleStorage.

class SimpleStorage<T> implements Storage<T> {
private T item;

@Override
public void store(T item) {
this.item = item;
}

@Override
public T retrieve() {
return item;
}

public static void main(String[] args) {
SimpleStorage<String> stringStorage = new SimpleStorage<>();
stringStorage.store("Hello, Generics!");
System.out.println("Stored String: " + stringStorage.retrieve()); // Outputs: Stored String: Hello, Generics!

SimpleStorage<Integer> intStorage = new SimpleStorage<>();
intStorage.store(42);
System.out.println("Stored Integer: " + intStorage.retrieve()); // Outputs: Stored Integer: 42
}
}

Explanation

  • Generic Interface Definition: interface Storage<T> defines a generic interface Storage with a type parameter T.

  • Method Signatures: The interface has two methods, store(T item) and retrieve().

  • Generic Class Implementation: class SimpleStorage<T> implements Storage<T> defines a generic class SimpleStorage that implements the Storage interface.

  • store(T item) Method: Stores an item of type T.

  • retrieve() Method: Retrieves the stored item of type T.

  • Using the Generic Class:

  1. SimpleStorage<String> stringStorage = new SimpleStorage<>(); creates a SimpleStorage for String.
  2. SimpleStorage<Integer> intStorage = new SimpleStorage<>(); creates a SimpleStorage for Integer.

Real-Life Example

  • Consider a real-life scenario where you manage a storage system for various types of items, such as a box.

  • You might have a generic interface Storage and a class Box that implements this interface.

interface Storage<T> {
void store(T item);
T retrieve();
}

class Box<T> implements Storage<T> {
private T item;

@Override
public void store(T item) {
this.item = item;
}

@Override
public T retrieve() {
return item;
}

public static void main(String[] args) {
Box<String> stringBox = new Box<>();
stringBox.store("Generic Box");
System.out.println("Stored in Box: " + stringBox.retrieve()); // Outputs: Stored in Box: Generic Box

Box<Integer> intBox = new Box<>();
intBox.store(100);
System.out.println("Stored in Box: " + intBox.retrieve()); // Outputs: Stored in Box: 100
}
}

Explanation

  • Generic Interface Definition: interface Storage<T> defines a generic interface Storage with a type parameter T.

  • Method Signatures: The interface has two methods, store(T item) and retrieve().

  • Generic Class Implementation: class Box<T> implements Storage<T> defines a generic class Box that implements the Storage interface.

  • store(T item) Method: Stores an item of type T.

  • retrieve() Method: Retrieves the stored item of type T.

  • Using the Generic Class:

  1. Box<String> stringBox = new Box<>(); creates a Box for String.
  2. Box<Integer> intBox = new Box<>(); creates a Box for Integer.

Benefits of Generic Interfaces

  • Type Safety: The compiler ensures that only the specified type is used, preventing runtime type errors.

  • Code Reusability: Generic interfaces allow you to create flexible and reusable code without duplicating logic for different types.

  • Flexibility: Provides more control over the types that can be used in the interface.

  • Compile-Time Checking: Generic interfaces provide compile-time type checking, catching errors early in the development process.

Summary

  • Generic Interfaces: Interfaces that declare one or more type parameters to operate on various types.

  • Type Safety: Ensures that only the specified type is used, preventing runtime errors.

  • Code Reusability: Allows you to write a single interface that can handle different types of data.

  • Flexibility: Can be implemented by classes that use various types.

  • Compile-Time Checking: Errors are caught at compile time rather than at runtime.

  • Example: Storage<T> interface with Box<T> class implementing it to handle different data types.

Generic interfaces in Java enable you to create flexible and reusable code by defining methods that can operate on various types. They provide compile-time type safety, ensuring that your code is robust and error-free. Mastering generic interfaces is essential for writing versatile and maintainable code in Java.