@MustOverride annotation?

2019-03-24 16:57发布

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 anybody has a custom java annotation that could achieve the same effect. Essentially what i want is to push for subclasses to override a method in a superclass that itself has some logic that must be run-through. I dont want to use abstract methods or interfaces, because i want some common functionality to be run in the super method, but more-or-less produce a compiler warning/error denoting that derivative classes should override a given method.

7条回答
祖国的老花朵
2楼-- · 2019-03-24 18:03

... and if declaring a base class abstract is not an option you can always throw an UnsupportedOperationException

class BaseClass {
    void mustOverride() {
        throw new UnsupportedOperationException("Must implement");
    }
}

But this is not a compile-time check of course...

查看更多
登录 后发表回答