How can I make sure that a super method is called in a child?

If I have a public void send() {//some code} method in the class and it has a child of this class, there is also a public void send() {//some code} method, how can I guarantee that the child must call super.send () somewhere in the send () method, what is it trying to override?

I was curious about this because I wrote in the API where, if you don't call super this method when overriding it, it will throw an exception, telling me that I did not name the super method. Is it hardcoded or can it be done with some keywords in Java?

+6
source share
7 answers

You can't, but you can ...

 class MySuperClass { public final void send() { preSend(); // do the work... postSend(); } protected void preSend() { // to be overridden in by sub classes } protected void postSend() { // to be overridden in by sub classes } } 
+14
source

You can do this by adding an abstract method (you don't see another way):

 abstract class MyClass { public final void send() // forbid changing this. { // do something doSend(): } protected abstract doSend(); // no external calls } 
+5
source

http://en.wikipedia.org/wiki/Call_super

What you are trying to do is an anti-pattern; you can do this (many classes in the Java kernel), but you shouldn't - unless you have a really good reason for this.

With the exception of this bit, all of the answers provided here are correct.

+4
source

Conceptually, this is similar to "delegation to the child." To achieve this, the parent class must implement the final method, which calls the abstract method that the child is supposed to implement.

 abstract class Parent { public final void invoke() { // pre invoke code doInvoke(): // post invoke code } protected abstract doInvoke(); // child should implement this } 
+2
source

You cannot force a subclass to call base. One thing you can do is change the send method to the base (final) send and sendcore (virtual), which will be overridden by subclasses. A basic โ€œsendโ€ would set some flag indicating that โ€œsendcoreโ€ was not called, and then calls sendcore. When he returns, he can check if the child sendcore called the base class.

+1
source

Nothing is built into Java to force a call to a superclass method. One approach to this is to use private flags in the superclass along with the delegation method. Something like that:

 public class Super { private boolean inSend; private boolean superCalled; public final void send() { inSend = true; superCalled = false; doSend(); inSend = false; if (!superCalled) { throw new IllegalStateException("Failed to call super.doSend()"); } } protected void doSend() { if (!inSend) { throw new IllegalStateException("Cannot call doSend() directly"); } superCalled = true; // base class functionality } } 
0
source

There is no keyword that will provide this. In my opinion, you either

  • Provide the subclass with all the information (through protected methods or not) so that it completely overlaps and changes the send call itself or ...
  • Document the API so that it is known that they must ultimately call send via super . I would suggest that most people who override the superclass method will do this if the class class is abstracted in any case.
0
source

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


All Articles