I will reflect other answers and say that there is no forced way in which derived classes can override a non-abstract method. The whole point of creating an abstract method is to determine that a method with this signature must exist, but cannot be specified at a basic level and therefore must be specified at a derived level. If there is a working, non-trivial implementation (since it is not empty and does not just throw an exception or show a message) of the method at the basic level, then this is not strictly necessary in order to call the method from the consumer of the derived class for success. Thus, the compiler does not need to forcefully cancel a method that can successfully execute at the base or derived levels.
In situations where you want a derived class to override your working implementation, it should be pretty obvious that the base implementation does not do what the consumers of the derived class want; the base class either does not have sufficient implementation or is incorrect. In such cases, you must trust that the programmer receiving your class will know what it is doing, and thereby know that the method needs to be redefined, because it does not give the correct answer in the context of using its new object.
I can think of one thing you could do. This will require an abstract base with a sealed (final for Javaheads) default implementation. Thus, there is a basic implementation of the method that is easily accessible for use as if it were a “base” class, but in order to define another class for the new script, you must return to the abstract class and therefore have to redefine the method. This method may be the only abstract thing in the class, thereby allowing you to use the basic implementations of other methods:
public abstract class BaseClass { public abstract void MethodYouMustAlwaysOverride(); public virtual void MethodWithBasicImplementation() { ... } } public final class DefaultClass:BaseClass { public override void MethodYouMustAlwaysOverride() { ... }
However, this has two drawbacks. First, since you do not have access to the DefaultClass implementation through inheritance, you cannot extend the DefaultClass implementation, which means that to accomplish what DefaultClass does, plus a little more, you must rewrite the code from DefaultClass, violating DRY. Secondly, this only works for one level of inheritance, because you cannot force override if you allow inheritance from DerivedClass.
KeithS Oct 20 2018-11-11T00: 00Z
source share