Why doesn't Java allow overriding of static methods?

后端 未结 22 2698
谎友^
谎友^ 2020-11-21 05:49

Why is it not possible to override static methods?

If possible, please use an example.

相关标签:
22条回答
  • 2020-11-21 06:34

    Now seeing above answers everyone knows that we can't override static methods, but one should not misunderstood about the concept of accessing static methods from subclass.

    We can access static methods of super class with subclass reference if this static method has not been hidden by new static method defined in sub class.

    For Example, see below code:-

    public class StaticMethodsHiding {
        public static void main(String[] args) {
            SubClass.hello();
        }
    }
    
    
    class SuperClass {
        static void hello(){
            System.out.println("SuperClass saying Hello");
        }
    }
    
    
    class SubClass extends SuperClass {
        // static void hello() {
        // System.out.println("SubClass Hello");
        // }
    }
    

    Output:-

    SuperClass saying Hello
    

    See Java oracle docs and search for What You Can Do in a Subclass for details about hiding of static methods in sub class.

    Thanks

    0 讨论(0)
  • 2020-11-21 06:35

    Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

    There were two considerations driving Java's design that impacted this. One was a concern with performance: there had been a lot of criticism of Smalltalk about it being too slow (garbage collection and polymorphic calls being part of that) and Java's creators were determined to avoid that. Another was the decision that the target audience for Java was C++ developers. Making static methods work the way they do had the benefit of familiarity for C++ programmers and was also very fast, because there's no need to wait until runtime to figure out which method to call.

    0 讨论(0)
  • 2020-11-21 06:35

    Overriding in Java simply means that the particular method would be called based on the runtime type of the object and not on the compile-time type of it (which is the case with overridden static methods). As static methods are class methods they are not instance methods so they have nothing to do with the fact which reference is pointing to which Object or instance, because due to the nature of static method it belongs to a specific class. You can redeclare it in the subclass but that subclass won't know anything about the parent class' static methods because, as I said, it is specific to only that class in which it has been declared. Accessing them using object references is just an extra liberty given by the designers of Java and we should certainly not think of stopping that practice only when they restrict it more details and example http://faisalbhagat.blogspot.com/2014/09/method-overriding-and-method-hiding.html

    0 讨论(0)
  • 2020-11-21 06:36

    The following code shows that it is possible:

    class OverridenStaticMeth {   
    
    static void printValue() {   
    System.out.println("Overriden Meth");   
    }   
    
    }   
    
    public class OverrideStaticMeth extends OverridenStaticMeth {   
    
    static void printValue() {   
    System.out.println("Overriding Meth");   
    }   
    
    public static void main(String[] args) {   
    OverridenStaticMeth osm = new OverrideStaticMeth();   
    osm.printValue();   
    
    System.out.println("now, from main");
    printValue();
    
    }   
    
    } 
    
    0 讨论(0)
提交回复
热议问题