-3

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> {        
    @Override
    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?

4

1 Answer 1

0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.