2

Starting with Java 1.6, we can use @Override to mark the implementation of methods defined in the interface. I understand the values of having that annotation, which I use systematically. But can someone explain to me what on earth is there to "override" since the interface just defines a contract and doesn't provide a default implementation?

Lolo
  • 3,411
  • 4
  • 34
  • 45
  • 3
    http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – Jason Mar 28 '13 at 00:53
  • I think it is just to indicate that it is an implementation of an contract defined by somebody else – Arun P Johny Mar 28 '13 at 00:53
  • +1 for Jason and also you don't have to put this annotation when using interfaces. – drzymala Mar 28 '13 at 00:54
  • Think of the annotation overrides more of a market to indicate that the implemented method overrides or implements the method from another class or interface, otherwise we'd need another annotation called implements and overrides is simpler (as you could be overriding a method that is implemented from an interface) – MadProgrammer Mar 28 '13 at 00:55
  • With modern IDEs adding a tag whenever a declaration implements an abstract interface method, I feel like putting an `@Override` annotation on all of them is overkill. Obviously you still want the annotation if you are inheriting an interface method from a superclass which has its own non-`abstract` implementation details. – Andrew Bissell Mar 28 '13 at 00:57
  • @Jason Thanks for the pointer. In fact, the first answer says "I think it would be better to have a separate annotation (like "Implements"), but it's better than nothing." This is exactly my point: we don't override, we implement here. – Lolo Mar 28 '13 at 03:46
  • @MadProgrammer Yes, this is how I translate the override annotation: it "overrides or implements the method from another class or interface", which bothers me since these are not the same. I am interested in your "simpler". Simpler for what? The compiler? The programmer? I would disagree with the latter: I'd rather have two annotations to distinguish when I am overriding an existing implementation or not. I doubt it would be much harder for the compiler to support two annotations either. Would it really? – Lolo Mar 28 '13 at 03:58

3 Answers3

1

Yes, there is nothing to override and the only plausible explanation is that this was an offshoot of convention.

In the context of a class implementing an interface, you don't really need this since the compiler will come screaming if you fail to write code for all interface methods anyway. In this setting, the annotation works like a marker, no different than a comment.

Also, if it's interface-related, IDE's ought to stop including @Override it in auto-generated quick-fix method stubs.

On top of that they should generate an "unused" code warning if the @Override annotation is used on an implementation of an interface method.

Jops
  • 21,340
  • 13
  • 43
  • 59
0

Semantically, it only makes sense if you consider an interface to be a kind of empty base class instead of a completely different thing. And historically this is in fact the origin of the interface concept. C++ was a language that didn't have a native concept of "interface", but that supported giving a single class multiple base classes. Many people who used the language felt that having multiple base classes was too unwieldly to be useful - except in the case of base classes that had no implementation at all, and only method definitions. Subsequent languages, such as Java, formalized the concept of interface-only base classes.

j__m
  • 8,784
  • 1
  • 26
  • 49
0

I guess it might be useful in this situation.

interface Goable {
  void go(Location loc);
}

class Vehicle implements Goable {
  void go() {
    ...
  }

  void go(Location loc) {
    ...
  }
}

In the class Vehicle, using @Override can help identify the implemented method.

wannik
  • 11,312
  • 10
  • 39
  • 55
  • There are MANY advantages in using @Override. There is not my question. – Lolo Mar 28 '13 at 03:40
  • For example, if you have a code: **Goable** g = new Vehicle(); g.go(new Location()); If a programmer wants to change the behavior of the code, @ Override might make it easier to locate the method that should be changed (i.e., @Override void go(Location loc) not void go()) – wannik Mar 28 '13 at 10:47
  • Again: my question is not why @Override is useful. I know it is. – Lolo Mar 28 '13 at 13:15