0

So for a school project, I need to make a private method that implements the Iterator interface. I have my method SetIterator, and it looks like this:

  private class SetIterator implements Iterator<T> {
  private T[] items; 
  private int count; 
  private int current; 

  private SetIterator(T[] elements, int size) {
     items = elements;
     count = size;
     current = 0;
  }

  private boolean hasNext() {
     return (current < count);
  }

  private T next() {
     if (!hasNext()) {
        throw new NoSuchElementException();
     }

     return items[current++];
  }

However, I am not able to compile because I'm getting this error message:

ArraySet.java:311: error: next() in ArraySet.SetIterator cannot implement next() in Iterator
  private T next() {
            ^
attempting to assign weaker access privileges; was public
where T,E are type-variables:
T extends Comparable<? super T> declared in class ArraySet
E extends Object declared in interface Iterator
ArraySet.java:307: error: hasNext() in ArraySet.SetIterator cannot implement hasNext() in Iterator
private boolean hasNext() {
                  ^
attempting to assign weaker access privileges; was public

I've never really run into this before, so any help would be greatly appreciated.

1
  • 1
    Make next() public: public T next() {. Commented Feb 27, 2015 at 8:47

4 Answers 4

2

Iterator is an interface. Hence, its methods are public. You cannot implement the method and make it private.

Change the visibility to public.

public boolean hasNext() {
  return (current < count);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I guess I just figured that having a public method in a private class would violate encapsulation somehow. Thanks :D
2

The error is fairly self explanatory: next and hasNext are public in Iterator so you can't make them private. Change private to public and the error should go away.

Comments

0

The reason you're getting this error is because you cannot assign a "weaker" privilege to a method you override from a super class or implement from an interface.

Since methods defined on an interface in Java are public by default, the Java compiler prevents us from implementing these methods with a weaker access privilege such as protected or private. The reason it does so is because of an inherent principle of object oriented programming - an instance of a subclass should be generally usable where an instance of its super class is expected (checkout the Liskov Substitution Principle to get a better understanding of this concept).

To sum up, declare SetIterator's hasNext() and next() methods as public, since they implement the Iterator interface's methods.

Comments

0

you would not have encountered this Issue if you would have known the rules for Overrding inr Java .

Try to google on overriding rules in Java. start from here : http://java67.blogspot.com/2012/09/what-is-rules-of-overloading-and-overriding-in-java.html

Always remember the thumb rule : Never reduce the scope of method , you can always increase the scope of method while overriding it , but can never decerese it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.