Raheel
Raheel

Reputation: 5163

Interface Vs anonymous class in android

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???

Upvotes: 0

Views: 1150

Answers (3)

Alex Lockwood
Alex Lockwood

Reputation: 83303

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.

Upvotes: 1

ditkin
ditkin

Reputation: 7044

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

Upvotes: 2

Related Questions