Require override of method to call super

前端 未结 7 2369
既然无缘
既然无缘 2020-12-17 08:11

I want that when a child class overrides a method in a parent class, the super.method() is called in that child method.

Is there any way to check this a

7条回答
  •  感动是毒
    2020-12-17 08:45

    unfortunately, there's no way to require it at compile time, and no way to detect it at runtime (unless there is some app specific logic or sequence of events that you could use to detect the method not being called). best thing to do is heavily document the method.

    if you really, really want to go down this path, you could use something like this pattern:

    public class BaseClass {
    
      public final void myMethod() {
        // do special stuff here which must always happen ...
    
        // allow subclasses to customize myMethod() here
        myMethodCustom();
      }
    
      protected void myMethodCustom() {
        // base class does nothing
      }
    }
    

提交回复
热议问题