Java override non abstract method as abstract in extended class

Tags: , ,



So I have a non-abstract method onStop inside the base class. Is it acceptable to make it abstract in the extented MyTask? The aim is to force the onStop to be implemented by classes that extend the MyTask.

public abstract class Task {

    public void onStop() {
    
    }
}

Implementation:

public abstract class MyTask extends Task {
    //..

    // Is this acceptable?
    public abstract void onStop();
}

Answer

It’s allowed to do that if MyTask is also abstract. It forces all the concrete sub-classes of MyTask to supply their own implementation of onStop() instead of using the onStop() implementation of the base Task class.



Source: stackoverflow