I know that abstract classes cannot be instantiated. But I have a doubt in the code below. This code is a part of android bitmap fun demo ( http://commondatastorage.googleapis.com/androiddevelopers/shareables/training/BitmapFun.zip).
// ImageWorkerAdapter class is nested in another abstract class ImageWorker
public static abstract class ImageWorkerAdapter
{
public abstract Object getItem(int num);
public abstract int getSize();
}
//this snippet is seen in Images.java
public final static ImageWorkerAdapter imageWorkerUrlsAdapter = new ImageWorkerAdapter() {
@Override
public Object getItem(int num) {
return Images.imageUrls[num];
}
I cannot understand how it is possible to create an instance of an abstract class. Please help me to understand this code.
The code you show does not show an abstract class being instantiated.
It shows an anonymous class being instantiated. In this case, the anonymous class extends an abstract class. You can also implement interfaces in anonymous classes,
Runnablebeing a very common example.