-1

In the Java documentaion it states:

@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.

I have a class that implements an interface and the implemented method statements have the @Override annotation.
Does this mean (according to the docs) that the interface is a superclass of my class? But how can it be, considering that interfaces only contain 'empty' methods, not methods that actually do stuff?

My code is LibGDX-related:

public class MouseAndKeyboard implements ApplicationListener {
    private Texture plane1;
    private Sprite mainPlane;
    private SpriteBatch mainSpritePacket;

    @Override
    public void create() {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        ...
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
jdurston
  • 199
  • 2
  • 14
  • An Interface can only be extended by an Interface, but can only be implemented by either an Abstract class or a Concrete class – Sarthak Mittal Dec 01 '14 at 14:25
  • related : [How are java interfaces implemented internally? (vtables?)](http://stackoverflow.com/questions/4423968/how-are-java-interfaces-implemented-internally-vtables) – NiziL Dec 01 '14 at 14:26
  • Interfaces were created so that multi-inheritence is "possible" (with big limitations). So basically it could be considered a Superclass. You also have to look at it from the other way around: An abstract superclass can also have an abstract method with no methodbody at all! – ITroubs Dec 01 '14 at 14:27
  • 3
    Tutorials are *descriptive* documentation; Javadoc is *normative*. When in doubt, you must always check the normative documentation. – Marko Topolnik Dec 01 '14 at 14:28
  • 1
    Using @Override was not legal on inherited interface methods until Java 6. That section of the web page might be old text that has not been updated. – DHall Dec 01 '14 at 14:29
  • @MarkoTopolnik actually he DID look into the "normative" documentation which contained the sloppy definition which is in question. – ITroubs Dec 01 '14 at 14:30
  • @DHall is the first one to give a plausible explanation as to why the definition might be false... – ITroubs Dec 01 '14 at 14:31
  • @ITroubs: It *could* be considered that, but it isn't. An interface is a super-*type*, not a super-*class*. – Oliver Charlesworth Dec 01 '14 at 14:31
  • 1
    @ITroubs How do you know that when the link points to the tutorial page.? Furthermore, DHall is wrong on the version: the change did not happen until Java 7. – Marko Topolnik Dec 01 '14 at 14:33
  • @MarkoTopolnik oh my mistake. You are right. Looking into the doc it states supertype for the `@Override` annotation. – ITroubs Dec 01 '14 at 14:36
  • 1
    @MarkoTopolnik no it was java 6. if you're going to call me out at least check your facts – DHall Dec 01 '14 at 14:41
  • @DHall [That is precisely what I did](http://docs.oracle.com/javase/6/docs/api/java/lang/Override.html). What is your source of facts? – Marko Topolnik Dec 01 '14 at 14:42
  • @MarkoTopolnik I just doubled-checked with IBM JDK 6 and `@Override` is supported there for interfaces. I'm pretty sure this is also the case for Oracle JDK 6, though I don't have the documentaton at hand. Sadly, the Javadoc of JDK 6 still mentions "superclass" not "supertype". – Puce Dec 01 '14 at 14:48
  • @Puce Well, this question is precisely about the difference between normative and descriptive :) `javac` may have *informally* allowed that, but it was non-compliant with the specification in that respect. Java 6 as a specification did not allow it. – Marko Topolnik Dec 01 '14 at 14:51

4 Answers4

4

For questions like this it's best to check the Javadoc. And the Javadoc mentions "supertype" not "superclass": https://docs.oracle.com/javase/8/docs/api/java/lang/Override.html

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Right! Good spot. I don't know what the definition of supertype is now though :( – jdurston Dec 01 '14 at 14:39
  • 1
    Either an extended class or an implemented interface. – Gimby Dec 01 '14 at 15:13
  • @Gimby I don't know the definite specification, but e.g. an extended interface is also a supertype (e.g. https://docs.oracle.com/javase/8/docs/api/java/io/Closeable.html) – Puce Dec 01 '14 at 15:34
1

It's still called an interface, it prevents you from making mistakes, so you should use it however.

Checkout the JSR http://docs.oracle.com/javase/specs/#9.6.4.4

Chapter 9.6.4.4 @Override

also:

Should we @Override an interface's method implementation?

Community
  • 1
  • 1
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
1

@Override always bothered me also, because it is used for both overriding the actual superclass method, and implementing an interface method. So, the documentation in taht part isn't written in the best way.

considering that interfaces only contain 'empty' methods

This isn't true anymore, in Java 8 interfaces can contain default implementations for declared methods.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • 2
    It hasn't "always" been that way; `@Override` was originally introduced in Java 5 with semantics strictly applying to overrides. In Java 7 this was extended to implementations because that way the annotation became much more useful. Its misleading name stays with us, unfortunately. – Marko Topolnik Dec 01 '14 at 14:31
  • Asuming interfaces are used to provide the means of multi-inheritence in Java, then @Override is not misleading at all – ITroubs Dec 01 '14 at 14:32
  • @ITroubs Common sense and the Java Language Specification agree that "override" does not apply to implementing interface methods. Common sense and the JLS diverge, however, when it comes to abstract methods defined by a class: these are "overridden" as far as the JLS is concerned. – Marko Topolnik Dec 01 '14 at 14:38
  • @MarkoTopolnik Yes, my use of `always` wasn't the best, just referring to the fact that this inclarity has been around for a while. – Predrag Maric Dec 01 '14 at 14:39
  • @ITroubs `multi-inheritance` via interfaces is also misleading imho – Predrag Maric Dec 01 '14 at 14:42
0

The explaination in the documentation is a bit sloppy. An Interface is not a superclass because a class can only have one superclass but implement multiple Interfaces. But you should still use the @Override Annotation for interface methods to show that this method ins inherited (there is no different Annonation only for interface methods).

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • Looking at it from a purely OO perspective an interface is the fake version of multi inheritence – ITroubs Dec 01 '14 at 14:28