Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating instance of static abstract class

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.

like image 287
Vysakh Prem Avatar asked Jan 23 '26 10:01

Vysakh Prem


1 Answers

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.

like image 145
Mik378 Avatar answered Jan 24 '26 23:01

Mik378