Why doesn't Java allow overriding of static methods?

后端 未结 22 2730
谎友^
谎友^ 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: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();
    
    }   
    
    } 
    

提交回复
热议问题