user2486322
user2486322

Reputation: 867

A constructor in an interface?

I’m reading an article about inner class. I found an example that demonstrates anonymous inner class (mentioned below).

  button1 = new JButton();
  button2 = new JButton();
  ...
  button1.addActionListener(
     new java.awt.event.ActionListener()
     {
        public void actionPerformed(java.awt.event.ActionEvent e)
        {
           // do something
        }
     }
  );

According to the example it creates an inner class for responding to a button using ActionListener interface. As I know an interface does not have a constructor. But I’m wondering, how they call a constructor.

"new java.awt.event.ActionListener(){ }"

Upvotes: 1

Views: 364

Answers (8)

Aniket Thakur
Aniket Thakur

Reputation: 69035

That is how Anonymous Classes are(syntax wise).

According to the docs

The anonymous class expression consists of the following:

  1. The new operator
  2. The name of an interface to implement or a class to extend.
  3. 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.
  4. A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

Upvotes: 0

Veronica Cornejo
Veronica Cornejo

Reputation: 488

You are not instantiating an interface. You are asking the compiler to create an anonymous class implementing that interface and immediately create an instance of this class.

The best way to demonstrate this is to go to the "class" directory. You will find files of the form className$1.class, className$2.class, etc. These files correspond to those anonymous classes. If you were instantiating the interface itself, there would be no need for these new class files (and the anonymous classes they contain, of course).

Upvotes: 0

Prabhaker A
Prabhaker A

Reputation: 8483

new java.awt.event.ActionListener(){ }
This statement creates an anonymous class object that implements ActionListener interface.
That is you are invoking anonymous class default constructor not the interface one.
According to java docs

The anonymous class expression consists of the following:
1.The new operator
2.The name of an interface to implement or a class to extend.
3.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.

Upvotes: 0

rocketboy
rocketboy

Reputation: 9741

Anonymous inner class: An inner class with no name.

Now the only detail here we care about is, this new class should subType the interface and for that we provide the necessary method implementations.

The constructor for this class is a default one and performs the job well because there are no instance variables associated.

Upvotes: 0

edwardsmatt
edwardsmatt

Reputation: 2044

Have a quick look at the Java Specification - specifically the Default Constructor Section. You get a constructor because when you instantiate an instance of an interface it would be an Object.

Quote from the spec:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727097

An interface does not have a constructor, but an anonymous class does: like all classes, it extends java.lang.Object implicitly, therefore it can call the Object's parameterless constructor.

Moreover, Object's constructor is the only constructor you could call, because anonymous classes cannot define custom constructors.

Of course in addition to extending java.lang.Object your anonymous class implements ActionListener - that's why you can pass it to the addActionListener method.

Upvotes: 2

Ray Tayek
Ray Tayek

Reputation: 10013

you are constructing a subclass.

Upvotes: 0

Related Questions