Reputation: 14744
I wonder if there is any way to avoid unnecessary methods from implemented interface or other class.
Let say that I have simple class implementing MouseListener
, when actually I need only one method mousePressed( MouseEvent e)
to override. However, in that case I also have to override every other method from interface but keep them empty like that:
public void mouseExited( MouseEvent e) {}
public void mouseClicked( MouseEvent e) {}
public void mouseReleased( MouseEvent e) {}
public void mouseEntered( MouseEvent e) {}
Is there any tricky solution to override only method I really need even when the others are abstract in interface/superclass and still keep my class as non-abstract?
Upvotes: 2
Views: 1857
Reputation: 438
The solution of JB Nizet is good but by the way why it is a problem ? with modern IDE like Eclipse you can add all methods to implement with just one click (source > override/implement method). If you use an adapter it's practical but you can't extend another class than the adapter.
Upvotes: 1
Reputation: 691635
Extend MouseAdapter
. It implements MouseListener
by doing nothing for every method. So you can just override the one that you want.
Upvotes: 8