Let's say I have a class Foo
implementing an interface such as MouseListener
. The MouseListener
interface consists of five methods but I only wish to override one of them (mouseClicked()
). Is there a standard, idiomatic way of formatting the other methods?
My inclination was to write the following:
@Override
public void mouseClicked(MouseEvent e) {
// (...) <-- actual code here
}
@Override
public void mouseEntered(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mouseExited(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mousePressed(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mouseReleased(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
I'm a fan of making it explicit that methods are intentionally blank rather than accidentally left so, but I'm not crazy about all the vertical space given up for basically nothing. I've also seen the following format:
public void mouseClicked(MouseEvent e) {
// (...) <-- actual code here
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
I'm generally OK with this and I understand the author's intent, but it gets really ugly when the (recommended) @Override
annotations are added.
I'm not a particularly experienced Java coder so I figured I'd ask if there was a convention. Thoughts?
I guess I'd describe it as "no-op implementations" or perhaps use the term "adapter". As others have noted, Java provides a
MouseAdapter
class which does what you want. Strictly speaking, it doesn't quite fall into the definition of the Adapter pattern, which transforms one API into another, but frankly, I tend to be pragmatic about naming such things.Probably the most important thing to do is be clear that you intend for the method to have no implementation. In the specific case of the
MouseAdapter
, you probably don't want to go around throwingUnsupportedOperationException
, but in general, it's probably a good signal that you don't intend to provide an implementation. A comment in the source code (or better, the method documentation) to explain just why you're not implementing the interface fully is usually necessary.I found this while searching for this exact question. I'm using on scroll where I need onScrollStateChanged but not onScroll. I was leaning towards:
However, I like the second example you give (with braces on same line). It's compact and clean and can consistently express the idea that it is intentionally left blank.
Edit: this is what I settled on:
This one has a lot of parameters so it doesn't look as nice as on one line but you get the idea.
Use MouseAdapter
MouseAdapter is great for this specific case and the Adapter idiom is great in general. An Adapter has empty implementations of all the methods of the interface, allowing you to subclass and implement only those methods that are relevant to your class. The Adapter could alternatively, as Andrew Hare suggests, throw NotImplementedException's.