简体   繁体   English

Java 将非抽象方法覆盖为扩展 class 中的抽象

[英]Java override non abstract method as abstract in extended class

So I have a non-abstract method onStop inside the base class.所以我在基础 class 中有一个非抽象方法 onStop。 Is it acceptable to make it abstract in the extented MyTask?在扩展的 MyTask 中将其抽象化是否可以接受? The aim is to force the onStop to be implemented by classes that extend the MyTask.目的是强制 onStop 由扩展 MyTask 的类实现。

public abstract class Task {

    public void onStop() {
    
    }
}

Implementation:执行:

public abstract class MyTask extends Task {
    //..

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

It's allowed to do that if MyTask is also abstract .如果MyTask也是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.它强制MyTask的所有具体子类提供它们自己的onStop()实现,而不是使用基本Task class 的onStop()实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM