Skip to main content
3 of 3
added 4 characters in body

Java 17: MenuIterator is not abstract and does not override abstract method remove() in java.util.Iterator

I'm getting the following error while implementing the java.util.Iterator interface in my class:

java: MenuIterator is not abstract and does not override abstract method remove() in java.util.Iterator.

import java.util.Iterator; public class MenuIterator implements Iterator<MenuItem> { private final MenuItem[] items; private int position = 0; public MenuIterator(MenuItem[] menuItems) { this.items = menuItems; } public boolean hasNext() { return position < items.length && items[position] != null; } public MenuItem next() { return items[position++]; } } 

remove() method is a default method in the Iterator interface, as well as forEachRemaing() method.

 default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } 

However, I am not forced to implement forEachRemaing(), but I do have to implement remove(). Also, the code works without implementing remove() if I run it from Eclipse, but gives an error in IntelliJ IDEA.

Does anyone know why I would need to provide an implementation for the default remove() method? Why should I provide it for one method and not for another? And why does it work without implementation in one IDE and not in another?