Java how to optional override method in abstract class?

前端 未结 5 1104
小鲜肉
小鲜肉 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:36

    Only abstract class (including interface) is not expected to declare ALL the methods from its base class.

    So, for instance an interface or abstract class extending one base class or implenting one interface hasn't to declare all the methods. The implementation of the non-implemented method will be the job of the first deeper concrete subclass.

    So for your problem, you eventually could use composition over inheritance adding the collaborator (containing your new common method) as a protected field of your base class.

    Thus, no need to implement nothing in your concrete classes and allowing these ones to use collaborator's method within a proper subclass method.

    You may be interested by the Bridge pattern whose goal is (from wikipedia):

    The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently"

提交回复
热议问题