Skip to main content
1 of 3

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'm not forced to implement forEachRemaing() but I must implement remove(). Also, the code works without remove() implementation if I run it from Eclipse but it throws an error in IntelliJ IDEA.

Does anybody know why should I have to provide an implementation for a default remove() method? Why should I provide it for one method and not for the other one? And why is it working without implementation in one IDE and not in the other one?

Thanks!