Indrz
Indrz

Reputation: 131

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

Upvotes: 3

Views: 320

Answers (1)

Eran
Eran

Reputation: 393771

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.

Upvotes: 3

Related Questions