Implement an interface and override methods in Java?

后端 未结 6 971
旧巷少年郎
旧巷少年郎 2021-01-24 16:14

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

For instance if I have

public class Foo extend JFrame implements ActionListener, KeyListener {
          


        
6条回答
  •  粉色の甜心
    2021-01-24 16:24

    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
        }
    });
    

提交回复
热议问题