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 inside of private inner class a.doA(env)
with the following message.
The method doA(E) in the type A<E> is not applicable for the arguments (E)
It doesn't seem like accessibility issue because non-static inner class have an access to all instance variables of the outter class. It looks like I defined my generics wrong somewhere. Can anyone explain me what I am doing wrong here?
You've used the same generic parameter name for the inner class, so the type E
of the inner class is shadowing the E
of the outer class.
Remove generic parameter from the inner class, like this:
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);
}
}
}
The type of the enclosing class is part of the type of the inner class. FooInner
is already parameterized by E
, because it's part of the outer class; the explicit parameterization is redundant and incorrect, because it's actually trying to introduce a new type parameter using the same name as the existing one. Just remove the <E>
in private class FooInner<E>
, and you're golden.
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.