-1

There is Why doesn't Java allow overriding of static methods?, which alleges that overriding static methods is not allowed in java. Yet it seems to work in OpenJDK:

Compiling these two classes works when not using @Override, but fails when doing so. To reproduce this, the file Parent.java looks like this.

public class Parent {
    public static int getActivity() { return 1; }
}

and the file Child.java like this:

public class Child extends Parent {
    // @Override public static int getActivity() { return 2; } // fails
    public static int getActivity() { return 2; } // works

    public static void main(String ... args) {
        System.out.println((new Child()).getActivity());
    }
}

The error is when using @Override is

$ javac Child.java
Child.java:3: error: method does not override or implement a method from a supertype
@Override public static int getActivity() { return 2; }

When removing the @Override, the output is 2, of the Child method.

The javac is version javac 1.7.0_79 of the OpenJDK.

Where is the error? (the ideal would be to add @Override to static methods, but the answer that this is a bug in the OpenJDK or my thinking would be good, too)

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 6
    You cant override static method – skywalker May 25 '15 at 13:39
  • 3
    Static methods can't be overridden, but they can be redefined. These are two different mechanisms. If you extend a class with static methods, you can redefine a method by defining a method with the same signature in your subclass. Any callers would have to refer to your subclass to get the redefined method. I have used this in project and it works fine. – Tobb May 25 '15 at 13:39
  • 1
    @Tobb They cannot be redefined, they can only be hidden (which is what is happening in the example). – Kayaman May 25 '15 at 13:40
  • @Kayaman Maybe "redefined" isn't the correct word for it, but in essence that is what one is doing.. – Tobb May 25 '15 at 13:41
  • 1
    @TheLostMind: that *is* how hiding works though. In Java and C#, hiding can be circumvented by casting to the appropriate type. – Jeroen Vannevel May 25 '15 at 13:42
  • side note - `@override` does a *compile-time* check. So, if a *parent* doesn't define an *instance level* method, this *annotation* will throw an error when used in the child class. – TheLostMind May 25 '15 at 13:46
  • 1
    I don't see a reason to downvote this question, it has all the necessary information to answer it. Upvoted to bring it back to 0. – Tobb May 25 '15 at 13:46
  • *"which alleges that overriding static methods is not allowed in java. Yet it works in OpenJDK"* It works in OpenJDK? Your example proofs, that this is not the case. – Tom May 25 '15 at 13:57
  • @Tom: what failed was the `@Override`-Annotation. The compilation worked. (I was not aware of the difference between overriding and hiding) – serv-inc May 27 '15 at 10:40
  • @NevyanovL: I was not aware of *hiding* vs overriding. Concerning your statement, have a look at http://stackoverflow.com/a/14980158/1587329 – serv-inc May 27 '15 at 10:51
  • @user1587329 It failed due to the problem in overriding the method. The compiler checks every method with this method if it really overrides a method. Due to the missing annotation, no one told you, that you're not overriding anything there. So it was a misinterpretation about the situation and that something like that works in OpenJDK. Can happen :). – Tom May 27 '15 at 11:46

1 Answers1

2

You are not overriding anything here... You just defined a function in class Child called getActivity(), which is distinct and hides the function getActivity() in class Parent.

UPDATE: From this question:

that doesn't mean it is overriding. In this case, it's the rules in section 8.4.8.3 of the JLS, "Requirements in Overriding and Hiding":

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109