Java compilation error when using generics parameter in inner class

Please take a look at the code snippet below:

interface IFoo<E>{
    void doFoo(E env);
}

class A<E>{
    public void doA(E env){}
}

public class Foo<E> implements IFoo<E>{
    public A<E> a;

    @Override
    public void doFoo(E env) {
        a.doA(env);
    }

    private class FooInner<E> implements IFoo<E>{

        @Override
        public void doFoo(E env) {
            a.doA(env);
        }
    }
}

Eclipse complains about an inner private class a.doA(env)with the following message.

The method doA(E) in the type A<E> is not applicable for the arguments (E)

This does not seem to be an accessibility problem, since a non-static inner class has access to all the instance variables of the outter class. Looks like I misplaced my generics somewhere. Can someone explain to me what I'm doing wrong here?

+3
source share
2 answers

. FooInner E, ; , , . <E> private class FooInner<E>, .

+3

, E E .

, :

public class Foo<E> implements IFoo<E>{

    ...

    private class FooInner implements IFoo<E>{ // "E" here is the same "E" from Foo

        @Override
        public void doFoo(E env) {
            a.doA(env);
        }
    }
}
+6

All Articles