0

I am little bit confused regarding backend working of anonymous class, like if we have a button and we are setting onclickListener

Button B = (Button)findViewById(R.id.myButton);
B.setOnClickListener(new onClickListener(){ 
          public void onClick(View V){ 
              Log.v("","Hello world");
          }
 ));

What is here actually happening in backend ?Does this will implement interface of View.OnClickListener or something else???

Raheel
  • 4,953
  • 4
  • 34
  • 40

3 Answers3

2

Please look over this

How can an anonymous class use "extends" or "implements"?

http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
2

Yes it is an instance of new unnamed class that implements the OnClickListener interface.

ditkin
  • 6,774
  • 1
  • 35
  • 37
1

Anonymous classes must always extend a class or implement an interface.

b.setOnClickListener(new OnClickListener() { 
    public void onClick(View V) { 
        Log.v("", "Hello world");
    }
});

In this case, you are creating a new anonymous (unnamed) class that implements the View.OnClickListener interface. This works because the setOnClickListener method takes an argument of type View.OnClickListener.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • There is simple typo here that could be confusing. There is no onClickListener Interface. It is OnClickListener. – ditkin May 26 '12 at 18:32