Skip to content
Advertisement

How to implement a generic interface that extends another generic interface in Java?

I am new to Java and working on implementing generic interfaces and that’s where I’d love some help.

I want to implement a generic interface in Java that extends another interface. This is how the current interface structure looks like –

interface ItemsProviderInterface<ExtReq, ExtRes> 
extends GenericItemProviderInterface<ItemRequest, ExtReq, ExtRes, ItemResponse> {}


interface GenericItemProviderInterface<Req, ExtReq, ExtRes, Res>{
    ExtReq convertInput(Req req)
    Res convertResponse(ExtRes res)
    ExtRes request(ExtReq req)
}

I want to implement the ItemsProviderInterface interface and provide a definition for convertInput(), convertResponse(), request() methods.

Maybe something like –

class itemsProvider<> implements ItemsProviderInterface<>

I am not sure which generic type to define the class with.. the ones corresponding to ItemsProviderInterface or GenericItemProviderInterface. I read up a lot of blog posts and searched StackOverflow for a similar question but couldn’t find one. Any help is HUGELY appreciated.

Advertisement

Answer

I think it’s worth inspecting which identifier means what in this case. Let’s look at the base interface first GenericItemProviderInterface<Req, ExtReq, ExtRes, Res>.

Here all 4 type parameters Req, ExtReq, ExtRes and Res are type parameters. That’s important to remember, because they are not themselves types!

Then let’s look at the definition of the derived interface: ItemsProviderInterface<ExtReq, ExtRes> only has 2 type parameters: ExtReq and ExtRes. Note that despite the identical names, those don’t inherently have anything to do with the ones on the base interface. That connection comes now:

interface ItemsProviderInterface<ExtReq, ExtRes> 
extends GenericItemProviderInterface<ItemRequest, ExtReq, ExtRes, ItemResponse> 

This means that an ItemsProviderInterface is-a GenericProviderInterface with the 4 type parameters set like this:

  • Req of GenericItemProviderInterface is set to the actual type (class or interface) ItemRequest.
  • ExtReq of GenericItemProviderInterface is set to the type parameter ExtReq of ItemsProviderItnerface
  • ExtRes of GenericItemProviderInterface is set ot the type parameter ExtRes of ItemsProviderInterface
  • Res of GenericItemProviderInterface is set to the actual type ItemResponse.

So 2 of the 4 type parameters of the base-class are “fixed” to a specific type and the remaining two are left variable.

If you now implement ItemProviderInterface you could either also have 2 type parameters and route them to ExtReq/ExtRes or fix them to specific classes (or do 1-and-1, but that’s probably not useful for this specific example.

So either

class MyItemProvider<ExtReq, ExtRes>
    implements ItemProviderInterface<ExtReq, ExtRes> {...}

or

class MySpecificItemProvider
    implements ItemProviderInterface<MySpecificExtReq, MySpecificExtRes> {...}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement