Require override of method to call super

前端 未结 7 2379
既然无缘
既然无缘 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:54

    There's not a way to check for this at compile time. (However, I've heard suggestions that you could do this at compile time with annotations, although I've never seen a specific annotation solution and I don't know how it would be done.) For a way to throw an exception at run time, see this thread. Perhaps you can refactor your code so that the base class has a final method that includes the essential parts and calls a non-final method that does not require a super call.

提交回复
热议问题