5

I am new to programming, I have very basic knowledge in android. As I learned in java that an Interface cannot be instantiated And that new is a keyword in Java that indicates creating an instance. I came across following code in Android:

public class MyActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

    setContentView(R.layout.mylayout);
    findViewById(R.id.button1).setOnClickListener(mButton1_OnClickListener);
  }  

  //On click listener for button1
  final OnClickListener mButton1_OnClickListener = new OnClickListener() {
    public void onClick(final View v) {
        //Inform the user the button has been clicked
        Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();               
    }
  };
}

Above code OnClickListener is a public interface and onClick(final View v) is an abstract method, Here goes my question: OnClickListener being an interface, how is possible to create an instance of it by using the new keyword in the example above?

talz
  • 1,004
  • 9
  • 22
ARP
  • 603
  • 1
  • 11
  • 23
  • Or you can inherit this `interface` to a `class = Activity` and make it behave like a `OnClick` hanler. – hackp0int Dec 26 '13 at 06:41
  • I didn't get please could you make me more clear ? – ARP Dec 26 '13 at 06:56
  • `interface` gives a behavior to whom is inherits it. that way you can decide if your activity can give the `interface` a `click behavior`. – hackp0int Dec 26 '13 at 06:58
  • Then the above code would look some this like as follows public class MyActivity extends Activity implements OnClickListener { public void onClick(final View v) { //Inform the user the button has been clicked Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show(); }} .Am I correct ? – ARP Dec 26 '13 at 07:01

3 Answers3

5

Here you are not instantiating an interface you are doing by Anonymous inner class.Your are not creating the object of the interface .you are creating this for the Anonymous class which is you gonna create in next step inside {Anonymous inner class};.If want to know more details of this you have to through Anonymous inner class.

Check it out below links:

http://c2.com/cgi/wiki?AnonymousInnerClass

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

rupesh
  • 2,865
  • 4
  • 24
  • 50
  • Well I gone through java doc It says Anonymous inner class enable us to declare and instantiate a class at the same time. But I didn't understand, how an interface in linked to an Anonymous inner class – ARP Dec 26 '13 at 06:49
  • See here you are giving the reference of interface then you are creating a new object for a class and that class u gonna create in next step. That's all.Little bit tricky go through any tutorials so that u can understand better. – rupesh Dec 26 '13 at 06:53
1

It is called anonymous inner class. That concept is entirely different.Behind the scenes, The compiler automatically creates a new class which implements that onClickListenerinterface.

This below link has detailed answer for your question.

Creating object from OnClickListener interface

Community
  • 1
  • 1
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
1

After long study am able to get clarification on anonymous class.Basically Anonymous stands for NO NAME,So the class with out any name is an Anonymous class . Ex:

  final OnClickListener mButton1_OnClickListener = new OnClickListener() {
        public void onClick(final View v) {
            //Inform the user the button has been clicked
            Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();               
        }
      };

Lets take an example of above code .It is an anonymous implementation , where mButton1_OnClickListener is an object of anonymous class and this anonymous class implementing OnClickListener interface. Similer example - How can an anonymous class use "extends" or "implements"?

Community
  • 1
  • 1
ARP
  • 603
  • 1
  • 11
  • 23