1

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:

  1. Through empty methods, the whole code makes an unclean impression.
  2. Any confusion when passing the code to third parties.
  3. 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

GreenAtomic
  • 96
  • 11
  • 4
    Use a [`KeyAdapter`](https://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyAdapter.html) instead? then you only have to override the methods you want to – khelwood Aug 23 '18 at 13:19
  • Create an abstract class with the "empty" implementations. For your concrete classes extend that abstract class and override only the methods you need. – k5_ Aug 23 '18 at 13:20
  • 2
    Imho, the best you can do is to try to seek for an interface which does not declare behavior you don't need, because what we do see here is the violation of interface segregation principle: https://en.wikipedia.org/wiki/Interface_segregation_principle – nyarian Aug 23 '18 at 13:21
  • @Jesper Quite right. Thanks. – khelwood Aug 23 '18 at 13:22
  • 1
    Possible duplicate of [Why do I need keyPressed(), KeyRelesed and keyTyped() in a KeyListener? - Java](https://stackoverflow.com/questions/48101625/why-do-i-need-keypressed-keyrelesed-and-keytyped-in-a-keylistener-java) – user1803551 Aug 23 '18 at 13:25
  • So the KeyAdapter would work for my example. But in general to handle the unused override methods, the way k5_ suggested, would be a good soulution. Did I understand that right ? – GreenAtomic Aug 23 '18 at 13:26
  • @user1803551 don't think that's a duplicate. You are just referring to my example. The question itself was about the unused override methods in general. I'm sorry if I've been unclear – GreenAtomic Aug 23 '18 at 13:27
  • `KeyAdapter` uses the way described by k5_. It also might be a design issue that you've put too many things in the interface, when you're only using parts of it. Then you might be able to refactor it to multiple interfaces and choose the proper one. – Kayaman Aug 23 '18 at 13:33
  • Update java version to 8 and make the interface methods default. – DEVAS Aug 23 '18 at 13:35
  • @Devendra that's pretty ugly from a design point of view. – Kayaman Aug 23 '18 at 13:36
  • See also [How to implement only a part of an interface](https://softwareengineering.stackexchange.com/questions/80739/how-to-implement-only-a-part-of-an-interface). – user1803551 Aug 23 '18 at 13:39
  • And after you edited the question, the new target duplicate is [avoid implementation of a method which is there in interface - java](https://stackoverflow.com/questions/20541000/avoid-implementation-of-a-method-which-is-there-in-interface-java) – user1803551 Aug 23 '18 at 13:43
  • @kayaman I agree abstract class design is the best approach while you are writing the code from start but if working on legacy code then have to do some workarounds. – DEVAS Aug 23 '18 at 13:46

1 Answers1

3

There are several ways to solve this issue. The preferred way in my opinion is to create an abstract class with default implementations of all your methods. Instead of implementing your interface, you can then just extend your abstract class and only override the methods you need:

Given your interface:

interface FooBarBaz {
    void foo();
    int bar();
    String baz();
}

You create an abstract class implementing this interface and providing default implementations for all methods:

abstract class AbstractFooBarBaz implements FooBarBaz {
     @Override
     public void foo() {
         // NOP by default
     }

     @Override
     public int bar() {
         return 0; // 0 by default
     }

     @Override
     public String baz() {
         return null; // null by default
     }
 }

And then you can use it in your code like this and only override the methods you need:

FooBarBaz fooBarBaz = new AbstractFooBarBaz() {
    @Override
    public int bar() {
        return 9000;
    }
};

An alternative solution would be to use default methods in you interface. This is not considered good practice though, as interfaces should define behaviour, not implementation. And after all, if it's a 3rd party interface, you might not even be able to do that.

Patric
  • 1,489
  • 13
  • 28