As you know, some methods must be overwritten when implementing interfaces. In my case it happens quite often that I have to overwrite methods that I don't need. As a result, my classes are crammed with empty @Override methods.
Example:
@Override
public void keyPressed(KeyEvent ke) {
// TODO Auto-generated method stub
if(ke.getKeyCode() == KeyEvent.VK_END) {
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
In this example, I would like to close my program by pressing the "End" key on the keyboard. As soon as I implement the required "KeyListener" interface, I have to overwrite the methods "keyReleased" and "keyTyped". However, since I do not need these two methods, they remain empty in my class.
Example note:
Of course, these 3 lines of code are not a big problem. The implementation of the "MouseListener" interface would look completely different, where almost twice the number would have to be overwritten. Not to mention what it would look like when implementing multiple interfaces.
Problem:
- Through empty methods, the whole code makes an unclean impression.
- Any confusion when passing the code to third parties.
- In many places it is advised to remove unused code. In my case these empty @Override methods would be unused. (e. g. here)
What would be the best way to clean up these empty @Override methods and make my code look more "clean"?
Edit
Don't get confused by my example. The question itself was about the unused override methods in general and not about the problem in my example. I'm sorry if I've been unclear