Must implement the inherited abstract method
Asked Answered
H

1

5

My class implements ActionListener. I have implemented the following nested classes below:

JMenuItem mntmNew = new JMenuItem("New...");
    mntmNew.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            doNew(e); //calls to outer class for cleaner code
        }
    });
    mnFile.add(mntmNew);

    JMenuItem mntmLoad = new JMenuItem("Load...");
    mntmLoad.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            doLoad(e); //calls to outer class for cleaner code
        }
    });
    mnFile.add(mntmLoad);

//etc. for the rest of the menu system

However, Eclipse is still telling me that my class must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent e). Can you not implement override methods in a nested class in this way?

Hirsutism answered 15/6, 2013 at 21:35 Comment(2)
Down-vote canceled with an up-vote. I'm not sure why someone down-voted this question as it appears to be a valid question to me.Retouch
++ because of @HovercraftFullOfEels's answer:)Trix
R
8

Your question:

Can you not implement override methods in a nested class in this way?

The answer is no. Eclipse (actually Java) is complaining that while you're declaring your class as implementing ActionListener you're not giving your class the necessary actionPerformed(...) method in the class's own scope -- and this last part is very important. The class that implements the interface must implement all the interface's required methods in its own scope and not in nested classes. Note that this doesn't prevent you from nesting classes that also implement ActionListener or other interfaces, but regardless, the rule remains that a non-abstract class that implements an interface must override all of the interface's methods.

But since you're not using objects of your class as an ActionListener, the simple solution is to not declare your class as implementing the ActionListener interface. Problem solved. And actually you're far better off not having your GUI class implement your listener interfaces since combining them in one class is asking a class to do too much. In technical terms, it unnecessarily reduces a class's cohesion and risks increasing it's coupling reducing its readability and maintainability.

Retouch answered 15/6, 2013 at 21:38 Comment(2)
I had come to that conclusion (GUI should not implement the listener interfaces) after the research I had done, which introduced the problem I asked about. Interesting that the standard Oracle documentation seems to lack any kind of reference to how to correctly implement actionlisteners on a large menu system.Hirsutism
@Daniel: I think that the Oracle documentation is a good starting point to learn the rudiments of Swing, but that's as far as it goes. Myself, I'm struggling to learn the ins and outs of creating and maintaining large Swing applications and trying to keep it as clean as possible with regards to MVC, decoupling, and dependency injection. OOP articles and online video presentations are helping.Retouch

© 2022 - 2024 — McMap. All rights reserved.