Skip to content
Advertisement

Java override non abstract method as abstract in extended class

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

Advertisement

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement