3

Lets suppose I have code like this:

public interface JustAnInterface {
    void doSomething();
}

public class JustAnInterfaceImplementation implements JustAnInterface {

    @Override
    public static void doSomething() {

    }
}

Why does static doSomething() method shows the error "Method does not override method from its superclass"?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32
  • 7
    Because methods defined in an interface are non-static. Your question is basically circular. – user207421 Aug 09 '17 at 10:37
  • 1
    Static methods in general do not make sense to override. They belong to the class in which they are defined and in that class only, and do not form part of a hierarchy. – Michael Aug 09 '17 at 10:37
  • 1
    The method signature is different from the interface. – seenukarthi Aug 09 '17 at 10:37
  • 1
    Because you can't have two methods with same signature where one is static and other non-static. Remember that having non-static one is required by fact that you are implementing interface, so code like `Interface in = new YourImplementation(); in.interfaceMethod();` would be always valid. – Pshemo Aug 09 '17 at 10:40
  • 1
    Static methods are "early bound", the implementation of a static method call is known at compile time. Calls to interface methods are "late bound", the implementation is only known at runtime – lance-java Aug 09 '17 at 10:49
  • 1
    I appreciate the super quick accept ;-) – GhostCat Aug 09 '17 at 10:58

3 Answers3

7

One could say: because @Override and static simply do not go together.

Keep in mind: polymorphism only works for non-static methods. static means that your method is "attached" to the containing class.

In other words: you know everything about static method invocations at compile time. But the process of determining which overridden method to invoke happens at runtime. These two concepts do not go together in Java.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

All what GhostCat says is correct but the @Override annotation is not the direct cause of the compilation error.

Even by removing it, the method is not valid at compile time.

You cannot override or implement a method by adding the static modifier in the derived class as overriding/implementing is valid only for instance methods.
Try without the annotation :

public class JustAnInterfaceImplementation implements JustAnInterface {

    public static void doSomething() {

    }
}

You would have a compilation error more specific than which one with @Override

method does not override or implement a method from a supertype

You should get something like :

overriding method is static

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

The static and non-static methods have different signatures, and not simply because of the static.

The non-static method is secretly taking an extra parameter - the value of this.

If you're familiar with python, you can see this explicitly:

class SomeClass(object):
  def doSomething(self):
    pass // whatever method body

Here, the self parameter is just a regular parameter: you can invoke the method and pass this parameter yourself.

In Java, it's sort of the same. When you invoke the doSomething method, you implicitly pass one parameter to it, and that becomes this.

However, the static method takes zero parameters. So actually, they aren't override-equivalent, and hence you aren't overriding the method correctly.

The JVM would have to somehow determine that the parameter should be discarded. The language could have been written to allow this, but the designers took the easier route of just disallowing it (because there's no good reason to use it anyway).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243