Implement an interface and override methods in Jav

2020-04-20 06:23发布

Why do you have to override all methods of an interface?

For instance if I have

public class Foo extend JFrame implements ActionListener, KeyListener {
      foo(){
      }
    @Override
    public void keyPressed(KeyEvent arg) {          
    }

    @Override
    public void keyReleased(KeyEvent arg) {
}

    @Override
    public void keyTyped(KeyEvent arg) {        
    }
}

I'm going to have lots of methods I won't even be using, is there a way to remove the un used implemented methods, for instance if i plan to use one method from the interface

I don't want to use abstract either, as this means I can't create an instance of the object (at least my compiler says so)

6条回答
够拽才男人
2楼-- · 2020-04-20 06:39

Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:

addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
        // handle typed key here
    }
});
查看更多
Summer. ? 凉城
3楼-- · 2020-04-20 06:39

If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?

However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.

Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.

查看更多
女痞
4楼-- · 2020-04-20 06:55

Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?

查看更多
神经病院院长
5楼-- · 2020-04-20 06:55

You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.

查看更多
劳资没心,怎么记你
6楼-- · 2020-04-20 06:57

This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):

public class Foo extend JFrame {
      foo(){
          ...
          component.addKeyListener(new MyKeyListener());
          ...
      }

    private class MyKeyListener extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent arg) {          
        }
    }
}
查看更多
倾城 Initia
7楼-- · 2020-04-20 06:59

You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.

When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.

查看更多
登录 后发表回答