Java how to optional override method in abstract class?

[亡魂溺海] 提交于 2020-05-25 03:30:23

问题


Let's say we have a base class:

public abstract class BaseFragment extends Fragment {
    ...
    protected abstract boolean postExec();
    ...
}

And then derive from it to have other class(es) (e.g. Fragment_Movie, Fragment_Weather ...)

public class Fragment_Music extends BaseFragment{
    @Override
    protected boolean postExec() {
        return false;
    } 
}

However, when adding a new method to the base class:

public abstract class BaseFragment extends Fragment {
    ...
    protected abstract boolean postExec();
    protected abstract boolean parseFileUrls();
    ...
}

Eclipse instantly shows up error asking to implement this new method in the already derived classes.

Is there away to add a "default" abstract method in the base class, so that it does not show error even if we don't implement it in the derived class? (because it'd take lot of time to fix each derived class every time the base class appends new method. )


回答1:


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");
    }
}



回答2:


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"




回答3:


Abstract methods purpose is to force you to implement them.

If add an abstract method in your superclass, you have to refactor your code and provide implementation; your program will be inconsistent even with default implementations - what if you need to return a custom type? I'd take my time to provide proper implementation, as I will probably need to call such methods if I added them.




回答4:


You could derive a new abstract class with the new abstract method from your base class. Derive concrete classes that should have the new method from this new class and others from the old base class.




回答5:


make your public BaseFragment class abstract

 public abstract class Fragment_Music extends BaseFragment{
              @Override
protected boolean postExec() {
    return false;
} 

    }


来源:https://stackoverflow.com/questions/12620365/java-how-to-optional-override-method-in-abstract-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!