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.
This code represents the initialization of an anonymous class extending ImageWorkerAdapter abstract class:
new ImageWorkerAdapter() {
@Override
public Object getItem(int num) {
return Images.imageUrls[num];
}
Indeed, the anonymous implementation is defined between the curly braces.
As being an anonymous class, it's perfectly valid to rely on an abstract class or interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With