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?
An empty interface in Java is known as a marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented. java. lang. Cloneable and java.
If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface. In short, you can access the default methods of an interface using the objects of the implementing classes.
Empty interfaces are used to mark the class, at run time type check can be performed using the interfaces. For example An application of marker interfaces from the Java programming language is the Serializable interface.
empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the stack is empty else it returns false.
In this particular case you should follow wilums2's advice and extend MouseAdapter instead of implementing MouseListener. The purpose of these adapter classes is so that you don't have to provide empty implementations when you're only implementing some of the methods of an interface.
More generally, the short answer is 'no', there is no standard convention for how to document empty methods, though I generally use something like
@Override void foo() { // No implementation necessary }
I do it the same way you do, if theres nothing there leave at one line. Maybe put a comment on top of a large block of 'implementation one-liners'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With