Re: @Override
On 7/23/2012 5:19 PM, Peter Duniho wrote:
[...]
I will admit that the number of times it's helped me avoid a mistake are
far and few between. But it's happened, and it's certainly no large
inconvenience to have to include "override" (in fact, at least with the
Visual Studio IDE, it's a convenience, as VS will pop up a list of
overridable methods, and then auto-generate a skeleton method to fill in
for the override...maybe Eclipse, NetBeans, etc. would do the same?).
NetBeans will do two things about @Override (maybe more). It
will flag an overriding method that lacks an @Override annotation
and suggest that you add it (which you can do with a shortcut).
A feature I find even more convenient is that it notices when you
attempt to intantiate an abstract class, and suggests that you
provide overrides for the missing methods. Another shortcut, and
presto! it writes skeletons for the missing methods, with @Override
already in place. Saves the bother of hunting up the exact spelling
of a method name.
Illustration: If I type
button.addActionListener(new ActionListener(){});
.... NetBeans tells me I've failed to implement abstract methods.
After Alt-Enter and a confirming Enter, it rewrites my code as
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException(
"Not supported yet.");
}
});
.... which I find very helpful, especially for interfaces that
have several abstract methods. (I've even been known to let
NetBeans expand MouseListener, then change it to MouseAdapter
and delete the auto-written methods I don't care about, just
to be sure I've got everything spelled properly.)
--
Eric Sosman
esosman@ieee-dot-org.invalid