Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why if static method don't involve in polymorphism(late binding) I see error that static method cannot be overridden

please consider following code:

class A{
    public static void m(Number n){
         System.out.println("Number A");
    };
}
class B extends A{
     public static int m(Number n){
        System.out.println("Number B");
        return 1;
      };
}

output:

java: m(java.lang.Number) in inheritanceTest.B cannot override m(java.lang.Number) in inheritanceTest.A return type int is not compatible with void

I know that static methods doen't involve in polymorphism hence I infer that overriding is impossible for my code. This compiler message is strange for me.

As I understand that overriding is part of polymorphism. I prepare for scjp and I am afraid make mistake in familiar question.

Please clarify this issue.

expected behaviour for me - message about overloading error

P.S1.

I have read top popular question about static overridden and I didn't found answer(

P.S2. According Pshemo answer:

this code:

class Foo{
    public static void m(Number n){
         System.out.println("Number A");
    };
    public static int m(Number n){
        System.out.println("Number B");
        return 1;
    };
}

outputs:

error: method m(Number) is already defined in class Foo
    public static int m(Number n){
                      ^
1 error

For me these situations are same. But compiler error is different - strange.

like image 594
gstackoverflow Avatar asked May 09 '14 20:05

gstackoverflow


People also ask

Can static methods be overridden and why why not?

Can we override a static method? No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.

Can we use static method in polymorphism?

We can declare static methods with the same signature in the subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'.

Can you declare an overridden method to be static if the original method is not static?

Can you declare the override method as static while the original method is non-static ? Answer: No, you cannot, the signature of the virtual method must remain the same, only the keyword "virtual" is changed to keyword "override" .

Can static methods be overloaded or overridden?

The static method is resolved at compile time cannot be overridden by a subclass. An instance method is resolved at runtime can be overridden. A static method can be overloaded.

Can we override static method in abstract class?

If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.

Can we override static method in interface?

Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.


1 Answers

JLS §8.4.8.3 (Java 8) says:

If a method declaration d1 with return type R1overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (§8.4.5) for d2, or a compile-time error occurs.

This same rule applies both to instance methods and static methods, since it says "overrides or hides". Basically, if you have a method with the same name and same parameters, it overrides if it's an instance method, but hides (the inherited method) if it's a class (static) method. And in both cases, the return type must either be the same or obey the rules for covariance.

Since it's the same rule, most likely there's just one place in the compiler code that checks this rule, and if the rule is violated you're getting the error you're seeing, which I'm sure is a much more common occurrence. The compiler really should check to see whether it should say "overrides" or "hides", but it looks like they slipped. Getting error message exactly right is not usually the highest priority of compiler writers--not compared to making sure code that's supposed to compile does so and runs right, and code that isn't supposed to compile doesn't. So I think this is a deficiency, but a very minor one.

like image 68
ajb Avatar answered Sep 28 '22 04:09

ajb