What happens when you declare a new instance of an Interface as follows:
OnClickListener oc = new OnClickListener(){
@Override
public void onClick(View v){
//Something
}
};
Where is the method onClick stored? Its got to be in some sort of class storage? Does this mean that this syntax creates an unnamed class and implicitly implements the OnClickListener interface to it? If so, how come you can assign different OnClickListener objects to each other? Surely as they are ‘implicitly’ different class types, this assignment of two different classes is impossible.
Could someone explain?
It’s called an Anonymous class.
If your code is in File
Foo.java, and you compile it, you would getFoo.classandFoo$1.classfor the first anonymous class. (Foo$2.classand so on for more anonymous classes).