In Java…
I am creating a class Foo which contains a method doAction(). My requirements:
- doAction() must have a default implementation (i.e. function body) in Foo.
- All subclasses of Foo must override doAction(), meaning that subclasses will get a compiler error if they do not provide a new implementation.
- I need to be able to instantiate Foo.
abstract would work, except that it does not allow me specify a function body for doAction().
Edit
It is impossible to simultaneously satisfy all of the requirements, end of story. You must give up at least one condition, and probably consider an entirely different approach to the problem you’re trying to solve.
Use two separate methods. Either:
Or just make the "required" method completely separate from the one you want to force subclasses to override: