Require override of method to call super

前端 未结 7 2352
既然无缘
既然无缘 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 09:07

    For the Android Developers out there, you can simply use the @CallSuper annotation.

    Sample:

    public class BaseClass {
      @CallSuper
      public void myMethod() {
        //do base required thing
      }
    }
    

    On the overriding class:

    public class OverridingClass extends BaseClass{
    
        @Override
        public void myMethod(){
            super.myMethod(); //won't compile if super is not called
            //do overring thing here
        }
    }
    

提交回复
热议问题