Arslan Ali
Arslan Ali

Reputation: 17802

Why do we use the "new" keyword with an Interface in Java?

I'm new to Android. I've studied in basic Object Oriented Programming courses that interfaces provide a way for classes to enhance their functionality. Classes who actually enhance their functionality this way, implement those interfaces and override all the methods written in interfaces.

Following code does the same job in Android:

public class MyActivity extends Activity implements OnClickListener {
   // All other code you may expect

   myButton.setOnClickListener(this);

   @override
   public onClick(View view) {
      // Code when view is clicked
   }
} 

This code is understandable. But the following code makes no sense to me, I've searched it over different places but not getting a satisfied answer.

public class MyActivity extends Activity {
   // All other code you may expect

   myButton.setOnClickListener(new OnClickListner() {

      @override
      public onClick(View view) {
         // Code when view is clicked
      }
  });
}

Now, OnClickListener() is an interface as said in Android documentation, and, now we are instantiating an interface. Not interfaces are implemented only? Please help me understand this point.

Upvotes: 4

Views: 3628

Answers (4)

Mjoellnir
Mjoellnir

Reputation: 486

Actually, this is one of the worst practices I've ever seen in OOP, not just java.

Basically, you create an anonymous inner class from an Interface, and you have to immediately implement all methods the interface provides. Consider the interface in this case like an abstract class (which it basically always is, more or less).

While widely spread, and properly supported by some languages, java does only support the concept of inner classes on language level. On byte code level however, there are no inner classes. The compiler rips these inner class language constructs out of their parent classes, places them as siblings besides them with a $-sign and a consecutive number, and elevates private scoped elements (methods, variables) to package scope. Now let that sink in for a moment and also think security implications.

Next thing is that these anonymous inner-classes, as excessively used as they are in the Android context, (the original question was tagged Android) totally clutter up the code and make it utterly unreadable. This is far away from clean code, something I strongly suggest to follow in order to maintain easily readable and maintainable code.

Another thing is that every new Interface() { ... }; naturally creates a new object. Usually you will see this with listeners in the Android world. Save yourself and your users some memory and implement one object with a switch if you a have multiple listeners...don't create a new object everytime just because the Google examples do it and because some people do it in their examples when answering here on SO and in tutorials or in forums. :)

Upvotes: 1

Dodge
Dodge

Reputation: 8307

if you use new with an interface, you have to override the methods providerd by the interface in the { } Block as you create an anonymous inner class that implements the interface but has no own name.

this allows you for example to have mutliple diffrent click handlers, without the need to create a class that implements OnClickListner for each button.

Upvotes: 1

Robby Pond
Robby Pond

Reputation: 73484

Those OnClickListeners are anonymous classes. The brackets include the class definition.

Upvotes: 1

PermGenError
PermGenError

Reputation: 46418

new OnClickListner() { is not instantiating an interface, it is declaring an anonymous inner class. basically an anonymous class(a class implementing the interface OnClickListner) which doesn't have a name per se.

From Documentation:

The anonymous class expression consists of the following:

  • The new operator
  • The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface OnClickListner.
  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: In the case of implementing an interface, there is no constructor, so you use an empty pair of parentheses, like in this example.
  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

Upvotes: 12

Related Questions