I am reading a book in which author used a code like this
public class Pool<T> {
public interface PoolObjectFactory<T> {
public T createObject();
}
private final List<T> freeObjects;
private final PoolObjectFactory<T> factory;
private final int maxSize;
public Pool(PoolObjectFactory<T> factory, int maxSize) {
this.factory = factory;
this.maxSize = maxSize;
this.freeObjects = new ArrayList<T>(maxSize);
} //end of constructor
} //end of class Pool<T>
Then he used code something like this
PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {
@Override
public KeyEvent createObject() {
return new KeyEvent();
} //end of createObject()
};
keyEventPool = new Pool<KeyEvent>(factory, 100);
I want to ask at the line PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {..}; he didn’t say implements PoolObjectFactory. Why? When you use interface then you use implements keyword?
Thanks
You are creating an anonymous class here. No need for the implements keyword. Note that in this case you use new PoolObjectFactory and this clearly indicates the class you are implementing – no need to indicate it once more using implements.
On the other hand if you are creating a subclass you need to state which is it’s parent interface and then you need the implements keyword.