Java how to optional override method in abstract class?

前端 未结 5 1100
小鲜肉
小鲜肉 2021-02-07 01:50

Let\'s say we have a base class:

public abstract class BaseFragment extends Fragment {
    ...
    protected abstract boolean postExec();
    ...
}
5条回答
  •  醉梦人生
    2021-02-07 02:29

    The easiest solution would be to add the method with a stubbed implementation. Declaring it abstract requires non-abstract extensions to implement the method.

    Doing something like this would ease your compilation problems, though it will obviously throw exceptions when used without overriding:

    public abstract class BaseFragment extends Fragment {
        protected boolean doSomethingNew() {
            throw new NotImplementedException("method not overridden");
        }
    }
    

提交回复
热议问题