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,723
  • 4
  • 32
  • 39

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,570
  • 3
  • 36
  • 36
2

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

ditkin
  • 6,401
  • 1
  • 32
  • 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
  • 82,434
  • 38
  • 201
  • 248
  • There is simple typo here that could be confusing. There is no onClickListener Interface. It is OnClickListener. – ditkin May 26 '12 at 18:32