I am implementing an interface I on a class A. Class A implements I.

I have an Iterator T that is supposed to return an object which implements I everytime I call next.

My iterator goes as follows (of course omitting other stuff):

public class T<I> implements Iterator<I> {        
    public I next() {
        return new A();

However, I get this error: Required I, found A (for the return new A() line)....

I don't know what is happening. Can someone help me?

closed as off-topic by gnat, Bart van Ingen Schenau, Kilian Foth, Jules, Jörg W Mittag Oct 12 '15 at 11:02

  • This question does not appear to be about software engineering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.


Your class definition introduces I as a type variable, meaning that it does not refer to the interface you expect it to. Change your class definition to

public class T implements Iterator <I>

and everything should work fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.