1

I am doing this problem in Leetcode.

The original code is:

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
public class NestedIterator implements Iterator<Integer> {

    public NestedIterator(List<NestedInteger> nestedList) {

    }

    @Override
    public Integer next() {

    }

    @Override
    public boolean hasNext() {

    }
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */

I realize that NestedIterator constructor use a list of interface as parameter. I feel confused why it doesn't implement the interface somewhere, but use it directly in list. I thought we shouldn't use interface directly. Thanks!

fourth
  • 599
  • 2
  • 5
  • 20

2 Answers2

0

As per task description You should not implement it (NestedInteger), or speculate about its implementation

In real world you have to declare interface and implement it somewhere before using. However in this simplified task you have to implement NestedIterator class only. LeetCode takes care of implementing NestedInteger in a proper way - all you need is to read the NestedInteger implementation notes carefully and use its methods in your code.

I feel confused why it doesn't implement the interface somewhere

It actually does. When compiling LeetCode will modify your code (e.g. by adding import some.package.NestedInteger) in such way that it does not throw compilation errors.

awesoon
  • 32,469
  • 11
  • 74
  • 99
  • In the real world, the implementation of the interface was done by an other team. – Johannes Kuhn Jul 30 '18 at 04:44
  • `However in this simplified task you have to implement NestedIterator class only.` What does this mean? `NestedIterator` is a class why I need to implement it? Is it syntactically correct to use interface name directly? – fourth Jul 30 '18 at 14:29
  • 1
    Implement does not necessary mean `implement` keyword. Implement means "write code for something" and sometimes it means "write code implementing interface". – awesoon Jul 30 '18 at 14:35
-1

Interfaces allow to abstract away implementation details. The using code does not need to know the concrete implementation it relies only on the interface. However, it is clear that an implementation of it must exist somewhere. One cannot instantiate an interface.

This is the power of interfaces and it is good practice to use them directly.

Henry
  • 42,982
  • 7
  • 68
  • 84