// Concrete implementation built atop skeletal implementation
static List<Integer> intArrayAsList(final int[] a) {
if (a == null)
throw new NullPointerException();
return new AbstractList<Integer>() {
public Integer get(int i) {
return a[i]; // Autoboxing
}
@Override
public Integer set(int i, Integer val) {
int oldVal = a[i];
a[i] = val; // Auto-unboxing
return oldVal; // Autoboxing
}
public int size() {
return a.length;
}
};
}
So far I knew we can not instantiate an abstract class at all . But what aren’t we doing the same thing here with return new AbstractList<Integer>() ? I am confused .
No, you are creating an anonymous class. You are subclassing your abstract class and you provide an implementation and instantiate it at the same time.
If you try this:
you will get an error since you won’t be providing a concrete implementation.
If you are confused you can always check out the official tutorials. Here it is:
Java Inner Classes