Non si può forzare una sottoclasse per eseguire l'override un metodo. Puoi solo forzarlo a implementare un metodo rendendolo astratto.
Quindi, se non si può fare myMotherClass astratto si può solo introdurre un altro superclasse che si estende myMotherClass e delega al metodo che deve essere implementata:
public abstract class EnforceImplementation extends myMotherClass {
public final void myMethod(){
implementMyMethod();
}
public abstract void implementMyMethod();
}
EDIT
ho trovato un altro modo interessting di risolvere il problema nell'API hemcrest
ad es usato da mockito.
public interface Matcher<T> extends SelfDescribing {
/**
* Evaluates the matcher for argument <var>item</var>.
* <p/>
* This method matches against Object, instead of the generic type T. This is
* because the caller of the Matcher does not know at runtime what the type is
* (because of type erasure with Java generics). It is down to the implementations
* to check the correct type.
*
* @param item the object against which the matcher is evaluated.
* @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.
*
* @see BaseMatcher
*/
boolean matches(Object item);
/**
* This method simply acts a friendly reminder not to implement Matcher directly and
* instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore
* compile errors .
*
* @see Matcher for reasons why.
* @see BaseMatcher
*/
void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}
L'interfaccia specifica un metodo _dont_implement_Matcher___instead_extend_BaseMatcher_
. Ovviamente non impedisce ad altri di implementare l'interfaccia Matcher
, ma guida lo sviluppatore nella giusta direzione.
E la classe BaseMatcher
implementa il metodo _dont_implement_Matcher___instead_extend_BaseMatcher_
come definitiva
public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// See Matcher interface for an explanation of this method.
}
Infine penso che questo sia un problema di progettazione, perché il BaseMatcher
implementa obviouosly logica che ogni Matcher
dovrebbe implementare. Quindi sarebbe stato meglio rendere la Matcher
una classe astratta e utilizzare un metodo di modello.
Ma credo che l'abbiano fatto perché era il miglior compromesso tra compatibilità con il codice byte e nuove funzionalità.
fonte
2013-08-20 09:28:00
In breve: non puoi senza renderlo astratto –
@stonedsquirrel Che dire di un'interfaccia? –
Questo non è possibile usando le annotazioni o lanciando un'eccezione se la classe non implementa il metodo? – Maniz