0

I have heard a lot that we cannot use Java's interface to instantiate it as a type but when I tried the following code it did not complain. Why?

OnClickListener lis1 = new OnClickListener() {

    @Override
    public void onClick(View v) {

    }
};

As OnClickListener is a interface, shouldn't it complain?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

0

That is an example of an Anonymous Class (excerpted),

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249