Java how to optional override method in abstract class?
Solution 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");
}
}
Solution 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"
markbse
Updated on May 18, 2020Comments
-
markbse almost 4 years
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. )
-
rizzz86 over 11 yearsRemove 'abstract' keyword from your method that you doesn't want to override
-
-
hage over 11 yearsBUt this way it wouldn't be possible to create objects of
Fragment_Music
-
PermGenError over 11 years@hage ofcourse, we cant instantiate abstract class. but you can always create an instance of the subclass(which implements this abstarct class) and call those methods. cant we ?
-
hage over 11 yearssure. we can instantiate the class that implements the abstract methods. But you made
Fragment_Music
abstract and it seems that this should have been the "implementing class", i.e. the class to instantiate