Search code examples
javagenericsinterfaceimplements

Is it possible to have two different generic types (such as T) in a Java interface?


I'm writing a generic interface representing a processor for several different types of custom objects, each of which is identified by either a string ID or an integer ID. I have written it in a way where T represents my custom objects (which all inherit from the same abstract class), but I need to take the IDs as parameters generically as well. Is there a way to have two generic types in a single class in Java?

I tried to write the simplest possible example of what I am trying to do below. "String/Integer" represents the two possible types of ID, which depends on what T is.

public interface Processor<T> {

    T update( String/Integer id );

    T find( Integer/Integer id );

    List<T> findAll( List<String/Integer> ids );

}

Thanks in advance for any advice!


Solution

  • Yes you can have multiple generic types.

    public interface Processor<T, S> {
        T update( S id );
        T find( S id );
        List<T> findAll( List<S> ids );
    }