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 get Foo.class and Foo$1.class for the first anonymous class. (Foo$2.class and so on for more anonymous classes).
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