Well, it seems that solidly formulating a question helps one to digest it better.
If we hold to the following facts:
- Objects of classes that implement an interface can be legally passed as arguments to methods where just the implemented interface is asked (without casting?).
- Interface variables can hold created objects of classes that implement this interface BUT (without explicit casting) have only access to the interface methods and only them (even though the object may be of a class with additional methods)
- If only needed once, objects of anonymous classes can be created on-the-fly where an interface is required by using the new keyword and the "Interface constructor" and automatically implement that interface.
So it seems that in my case, the following code:
setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
});
instantiates an anonymous class that automatically implements the AdapterView.OnItemClickListener interface, overrides in-line the interface onItemClick() method and passes the object to the setOnItemClickListener function.
To make it even clearer, the code above could have been written:
class classDef implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
}
classDef myClass = new classDef();
setOnItemClickListener(myClass);
Thank you.
does that mean that an object of every class that implements this interface can be passed as argument?yes