Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@MustOverride annotation?

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.

like image 822
Harrypotter2k5 Avatar asked Apr 30 '10 05:04

Harrypotter2k5


People also ask

Why is @override used?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.

What means @override in Java?

The @ is Java Annotations. The @Override means that the method is overriding the parent class (in this case createSolver ). The Javadoc states for @Override : Indicates that a method declaration is intended to override a method declaration in a superclass.

Do you need @override in Java?

It is not necessary, but it is highly recommended. It keeps you from shooting yourself in the foot. It helps prevent the case when you write a function that you think overrides another one but you misspelled something and you get completely unexpected behavior.

What happens if we remove @override?

Functionally, there is no difference. However, with the annotation, you get the added checks that there is a run() method to override, which can catch bugs where you aren't using the interface you think you are using.


3 Answers

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.

like image 125
StaxMan Avatar answered Sep 28 '22 07:09

StaxMan


Ignoring abstract methods, there is no such facility in Java. Perhaps its possible to create a compile-time annotation to force that behaviour (and I'm not convinced it is) but that's it.

The real kicker is "override a method in a superclass that itself has some logic that must be run through". If you override a method, the superclass's method won't be called unless you explicitly call it.

In these sort of situations I've tended to do something like:

abstract public class Worker implements Runnable {
  @Override
  public final void run() {
    beforeWork();
    doWork();
    afterWork();
  }

  protected void beforeWork() { }
  protected void afterWork() { }
  abstract protected void doWork();
}

to force a particular logic structure over an interface's method. You could use this, for example, to count invocations without having to worry about whether the user calls super.run(), etc.

like image 33
cletus Avatar answered Sep 28 '22 05:09

cletus


... 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...

like image 26
armandino Avatar answered Sep 28 '22 06:09

armandino