I understand:
Well actually you can - but only if you implement any methods that have been declared abstract or left out.
/**
* A classic adaptor pattern.
*
* @param
* @param
*/
public static interface Adapter {
public Q adapt(P p);
}
/**
* An I walks an iterator of one type but delivers items of a different type.
*
* Please fill in the `next()` method. Use an Adaptor for convenience.
*
* @param
* @param
*/
public abstract static class I implements Iterator {
protected final Iterator it;
public I(Iterator it) {
this.it = it;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public void remove() {
it.remove();
}
}
/**
* Use an adaptor to transform one type into another.
*
* @param
* @param
*/
public static class IA extends I {
private final Adapter adaptor;
public IA(Iterator it, Adapter adaptor) {
super(it);
this.adaptor = adaptor;
}
@Override
public T next() {
// Implement the abstract method on-the-fly.
return adaptor.adapt(it.next());
}
}
The IA class instantiates an object of the I abstract class and implements the next method that was missing from the I class. You are actually creating an object of an anonymous that implements the implied abstract method.