frajerko1 frajerko1 - 2 months ago 12
Java Question

Android - Java - We cant instantiate Inteface - Whats Cursor then

Hello i found an information about interfaces in java and it says we cant create an instance of it

but i am currently implementing SQLite and i have found this :

Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);


meanwhile :
public interface Cursor extends Closeable


so i am able to create an instance of interface?

Answer

No you aren't creating an instance by doing that. Creating an instance requires use of the new keyword (or using reflection but thats out of the scope of the question). Somewhere inside db.rawQuery they are creating an instance of a class that implements Cursor. Implementing an interface requires that you have all of the methods it declares implemented (go figure), meaning you can treat that object as though it were an instance of a "Cursor class", and return it to someone expecting a Cursor. They aren't ever directly creating an instance of that interface.

Comments