Eric
Eric

Reputation: 248

Can Java interfaces have constructors?

ActionListener is a interface but why can i create instance object?

   JButton button = new JButton("Button1");

   ActionListener me = new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            JOptionPane.showMessageDialog(null,ae.getActionCommand());  
        }
    };
    button.addActionListener(me);

Or what else? I am not sure. Please help me.

Upvotes: 0

Views: 3097

Answers (9)

whaley
whaley

Reputation: 16265

What you have instantiated is an Anonymous Inner Class. In short, it's an in-line way to both define a class that has no name and instantiate an instance of that class in one statement. You'll only ever be able to refer to anonymous inner classes by the super class they implement or extend. In the case of this question, the super class is the ActionListener interface.

When you compile your code, there will be an extra .class file that exists with a name like this: OuterClass$1.class. That is the class file that represents the anonymous inner class you've defined.

If you want to learn more, check out this section in the JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. You can't have constructor in Interface in java.

2. What you saw here is an Anonymous Class, which is declared and initialized simultaneously, and it must extend or implement a class or interface respectively.

Upvotes: 0

DanChianucci
DanChianucci

Reputation: 1175

ActionListener is in fact an interface which can not be instantiated.

However, by defining public void actionPerformed() locally you are allowing the interface to act like a class.

This is legal:

 ActionListener me = new ActionListener(){
      public void actionPerformed(...){...};
 };

This is not:

ActionListener me = new ActionListener();

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

You are not creating an instance of ActionListener. You are creating an anonymous class which implements ActionListener and you are providing that implementation.

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298918

Actually, what you are creating is an anonymous subclass of Object.class that implements the interface. So you are "inheriting" the Constructor from Object, not from the interface.

Upvotes: 1

Dan D.
Dan D.

Reputation: 32391

ActionListener itself is an interface, indeed.

However, the construct in your code is an anonymous inner class, meaning that your interface was implemented by that inner class.

Upvotes: 1

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

Unlike say C#, Java's interfaces cannot prescribe a constructor.

What you are doing in your code is creating an anonymous class that extends java.lang.Object (which does have a default constructor) and implementing the interface.

Upvotes: 4

Quinma
Quinma

Reputation: 1476

Because you're implementing the interface with your anonymous class

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308061

What you're seeing here is called an anonymous class: me will be assigned an instance of an anonymous (un-named) class that implements the ActionListener interface.

Upvotes: 6

Related Questions