Write an implementation of a method in a base class that requires overriding in subclasses?

In Java ...

I am creating a Foo class that contains the doAction () method. My requirements:

  • doAction () must have a default implementation (i.e. function body) in Foo.
  • All subclasses of Foo must override doAction (), which means that subclasses will receive a compiler error if they do not provide a new implementation.
  • I need to create an instance of Foo.

abstract will work, except that it does not allow me to specify the function body for doAction ().

+4
source share
5 answers

Edit

It is impossible to simultaneously satisfy all the requirements, the end of the story. You should give up at least one condition and probably consider a completely different approach to the problem you are trying to solve.


Use two separate methods. Or:

abstract class Foo { // Override this method abstract void doActionInSubclass(); // You can't override a final method // And you don't want subclases to override this one final void doAction () { // do whatever default-y things you want here doActionInSubclass(); } } 

Or just make the β€œrequired” method completely separate from the one you want to force the subclasses to override:

 abstract class Foo { abstract void mustOverrideThisInConcreteSubclasses(); final void doAction() { // default-y things here } } 
+5
source

If you give a default implementation to a method in Java, you cannot force subclasses to override it again. If you can, use the template method template using a different method name:

 public class Foo{ public abstract void templateMethod(); public final void doAction(){ //default implementation templateMethod(); // call template method } } 
+3
source

Add your method to Foo and make it always throw an exception, such as UnsupportedOperationException . This will require subclasses to override the method.

0
source

If all subclasses should override it, why do you want to provide a default implementation ?! it would be pointless, since no one would use it. However, you probably have your doAction code that you want to execute with any subclasses and some code that you want to override. In this case, you can create the following:

 doAction(){ //put your default code here... specificAction(); } abstract specificAction(); 

Thus, doAction has its own implementation, which ends with a call to specificAction, which must be implemented by subclasses

0
source

In the constructor (classes) of the Foo class, you can use reflection :

 class Foo { public Foo() { Class c = getClass(); if (!c.equals(Foo.class)) { // try/catch omitted for brevity Method m = c.getMethod("methodName", new Class[0]); if (m.getDeclaringClass().equals(Foo.class)) { throw new Exception("blah blah blah"); } } } } 

This will not lead to a compile-time error, but since the call to super() in the constructor cannot be wrapped with try , there is no way for anyone to do a subclass to get around it.

0
source

Source: https://habr.com/ru/post/1381026/


All Articles