@MustOverride annotation?

后端 未结 7 980
悲&欢浪女
悲&欢浪女 2021-02-12 22:40

In .NET, one can specify a \"mustoverride\" attribute to a method in a particular superclass to ensure that subclasses override that particular method. I was wondering whether

7条回答
  •  无人及你
    2021-02-12 23:07

    I don't quite see why you would not want to use abstract modifier -- this is intended for forcing implementation by sub-class, and only need to be used for some methods, not all. Or maybe you are thinking of C++ style "pure abstract" classes?

    But one other thing that many Java developers are not aware of is that it is also possible to override non-abstract methods and declare them abstract; like:

    public abstract String toString(); // force re-definition
    

    so that even though java.lang.Object already defines an implementation, you can force sub-classes to define it again.

提交回复
热议问题