简体   繁体   中英

Generic Type from Interface with Generic Type in Implementation

I have a very common use case dangling with Generics in JAVA and I can't focus how is this not possible.

The following example shows that it is not possible to create a Generic Type in Interface to further passing it as generic Type with in the implementing method.

interface MyInterface <T, I, O>
{
     public T method(T arg);
}    

abstract class MyAbstractClass <I , O> implements MyInterface<List<?>, I , O>
{
     // This cause a syntax-failure! What is wrong with this? 
     // And how can I manage my Generics like this?
     @override
     public abstract List<O> method(List<I> arg); 
}

class MyImplementation extends MyAbstractClass <String , Integer>
{
     List<String> method(List<Integer> arg)
     {
         // ... implementation
     }
}

I am not sure this is really what you want to do, but this way would work:

interface MyInterface <T, I, O>
{
    T method(I arg);
}

abstract class MyAbstractClass <O, I> implements MyInterface<List<O>, List<I> , O>
{
    @Override
    public abstract List<O> method(List<I> arg);
}

class MyImplementation extends MyAbstractClass <String , Integer>
{
    @Override
    public List<String> method(List<Integer> arg) {
        // ... implementation
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM