JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Can we override static method in Java or what is Method Hiding in java with example

Can we override static method in Java - which is also known as Method Hiding


Can we override static method in Java

No, you cannot override static method in Java because method overriding is based upon dynamic binding at runtime and static methods are bonded using static binding at compile time. Though you can declare a method with same name and method signature in the subclass which does look like you can override static method in Java but in reality that is method hiding. Java won't resolve method call at runtime and depending upon the type of Object which is used to call a static method, the corresponding method will be called. It means if you use Parent class's type to call a static method, original static will be called from a patent class, on their other hand if you use Child class's type to call a static method, the method from child class will be called. In short, you can not override static method in Java. If you use Java IDE like Eclipse or Netbeans, they will show a warning that static method should be called using class name and not by using object because static method can not be overridden in Java.



The overriding Static method in Java - Example

Can we override static method in JavaIn last section we saw the theory that we can not override static methods in Java, static method can only be hidden in subclass Let's see an example to test that theory which says you can not override static method in Java






public class CanWeOverrideStaticMethod {
 
    public static void main(String args[]) {
     
        Screen scrn = new ColorScreen();
     
        //if we can  override static , this should call method from Child class
        scrn.show(); //IDE will show warning, static method should be called from classname
     
    } 
 
}

class Screen{
 
    /*
     * public static method which can not be overridden in Java
     */
    public static void show(){
        System.out.printf("Static method from parent class");
    }
}

class ColorScreen extends Screen{
    /*
     * static method of the same name and method signature as existed in super
     * class, this is not method overriding instead this is called
     * method hiding in Java
     */
    public static void show(){
        System.err.println("Overridden static method in Child Class in Java");
    }
}

Output:
Static method from the parent class

This output confirms that you can not override static method in Java and static method are bonded based upon type information and not based upon Object. had Static mehtod be overridden, the method from Child class or ColorScreen would have been called.

.