On Mon, 07 Sep 2009 23:20:32 -0500, Mike Amling <mamling@rmcis.com>
wrote, quoted or indirectly quoted someone who said :
A certain class is used 80% of the time as is, and is extended 20% of
the time. I want to ensure that each subclass overrides a certain
method. The only way I can think of to do this is to put code in the
base class's constructors, and when the class being constructed is not
the base class, check that getDeclaredMethod does not throw a
NoSuchMethodException.
Does anyone know of a more efficient way? At compile time, maybe?
The other way out of this is to put an assert in the method that must
be overridden.
It could use this.class to figure out if it is working with a base
class or an extension. If they have overridden, the assertion won't be
there.
To get fancy you want to make it ok if they call super. You could look
at the stack. See http://mindprod.com/jgloss/trace.html
Others might have better ways of doing this with a run time check.
eschew reflection.
decide which behavior to use, you've messed up. (To nitpickers: that's the
You don't need a run-time check.