简体   繁体   中英

Removing non-implemented methods

When I implement an interface Eclipse tells me to add all implemented methods.

Since not all of them are usually needed, is there a way to delete methods inherited from the interface instead of having an empty body? It bothers me a bit to have this useless code floating around.

A good example for this is the KeyListener interface.

jTextComponent.addKeyListener(new KeyListener()
{
    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }

    @Override
    public void keyReleased(KeyEvent arg0)
    {
    }
});

I want to write the following or similar:

jTextComponent.addKeyListener(new KeyListener()
{
    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }
});

Is there a way to say that all non-implemented methods are automatically empty or something instead of putting them into the code?

There is not a way to do this with any interface.

However, in this particular case (with KeyListener ) there is a class KeyAdapter which implements all the methods with empty bodies, so that you can do this:

jTextComponent.addKeyListener(new KeyAdapter()
{
    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }
});

If you're curious, KeyAdapter is basically this:

public class KeyAdapter implements KeyListener
{
    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
    }

    @Override
    public void keyReleased(KeyEvent arg0)
    {
    }
}

If you implement an interface , you have to fulfill the contract by implementing all of its methods. If you do not want to implement methods then you can leave the method body blank, but still you will have to inlclude the methods in your code.

Is there a way to say that all non-implemented methods are automatically empty or something instead of putting them into the code?

No, there isn't , atleast at implementation class level.

But from java 8 onwards you can define default methods in interface, which also requires making changes in interface itself rather than implementing class. These methods provide flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.

No. You could use an abstract class between your interface and concrete class and add unused methods there. But Interface defines a contract, the set of basic properties / methods which a concrete class should have to be called as the type of the interface .

In eclipse, you click on the compiler error and click add unimplemented methods to add all the unimplemented methods in the class which implements the interface .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM