Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructors in Inner classes (implementing Interfaces)

How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this:

JButton b = new JButton(new AbstractAction() {

    public AbstractAction() {
        super("This is a button");                        
    }


    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler asks for return type). Does anyone have an idea?

like image 673
thepandaatemyface Avatar asked Jun 15 '10 12:06

thepandaatemyface


People also ask

CAN interface have inner class constructor?

Yes, you can define a class inside an interface.

Can we have constructor inside interface in Java?

No, you cannot have a constructor within an interface in Java. You can have only public, static, final variables and, public, abstract, methods as of Java7.

Are constructor Cannot be provided for which of the following inner classes?

An anonymous class is defined and instantiated in a single statement. Anonymous inner class always extend a class or implement an interface. Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class.

What is inner class interface?

Java inner class or nested class is a class that is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.


4 Answers

Just insert the parameters after the name of the extended class:

JButton b = new JButton(new AbstractAction("This is a button") {

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

Also, you can use an initialization block:

JButton b = new JButton(new AbstractAction() {

    {
       // Write initialization code here (as if it is inside a no-arg constructor)
       setLabel("This is a button")
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 
like image 121
Itay Maman Avatar answered Oct 05 '22 22:10

Itay Maman


If you really need a contructor for whatever reason, then you can use an initialization block:

JButton b = new JButton(new AbstractAction() {

    {
        // Do whatever initialisation you want here.
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

But you can't call a super-class constructor from there. As Itay said though, you can just pass the argument you want into the call to new.

Personally though, I would create a new inner class for this:

private class MyAction extends AbstractAction {

    public MyAction() {
        super("This is a button.");
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}

then:

JButton b = new JButton(new MyAction());
like image 36
DaveJohnston Avatar answered Oct 05 '22 22:10

DaveJohnston


The resulting class is not of type AbstractAction but of some (unnamed, anonymous) type that extends/implements AbstractAction. Therefore a constructor for this anonymous class would need to have this 'unknown' name, but not AbstractAction.

It's like normal extension/implementation: if you define a class House extends Building and construct a House you name the constructor House and not Building (or AbstractAction just to com back to the original question).

like image 28
Andreas Dolk Avatar answered Oct 05 '22 23:10

Andreas Dolk


The reason the compiler is complaining is because you are trying to declare a constructor inside your anonymous class, which is not allowed for anonymous classes to have. Like others have said, you can either solve this by using an instance initializer or by converting it to a non-anonymous class, so you can write a constructor for it.

like image 22
programmar Avatar answered Oct 05 '22 22:10

programmar