Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an idiom in Java for empty methods which exist to satisfy an interface?

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?

like image 522
camdez Avatar asked May 19 '09 18:05

camdez


People also ask

Can an interface be empty Java?

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.

Do you need @override for interface methods?

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.

What is a use for an empty interface?

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.

What are empty methods in Java?

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.


2 Answers

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 } 
like image 197
Dónal Avatar answered Sep 21 '22 14:09

Dónal


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'.

like image 34
Jan Gressmann Avatar answered Sep 22 '22 14:09

Jan Gressmann