Tuesday, August 28, 2012

Can we override static method in Java - 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 sub class 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 type of Object which is used to call static method, corresponding method will be called. It means if you use Parent class's type to call static method, original static will be called from patent class, on ther other hand if you use Child class's type to call static method, 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 warning that static method should be called using class name and not by using object becaues static method can not be overridden in Java.

Overriding Static method in Java - Example

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

/**
 *
 * Java program which demonstrate that we can not override static method in Java.
 * Had Static method can be overridden, with Super class type and sub class object
 * static method from sub class would be called in our example, which is not the case.
 * @author
 */

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 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 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, method from Child class or ColorScreen would have been called.

That's all on discussion Can we override static method in Java or not. We have confirmed that no, we can not override static method, we can only hide static method in Java. Creating static method with same name and mehtod signature is called Method hiding in Java.

Other Java tutorials from Java67 site :

8 comments:

  1. This explanation also clear this line-
    "Polymorphic method invocations apply only to overridden instance methods."

    ReplyDelete
  2. Javin Paul created a new blog on java..Coool.I love the way you write

    ReplyDelete
  3. is it possible to override main method in Java ? Since main method is static in Java, it looks we can not override main in Java but what will happen if I use varargs version of main method in Super class and String[] version of main method in SubClass, which main method will be invoked ?

    ReplyDelete
    Replies
    1. For your question "if I use varargs version of main method in Super class and String[] version of main method in SubClass, which main method will be invoked ? ",
      When you take different arguments in your method, irrespective of whether it is main() or some other method,it is called method "overloading", not overriding.

      Delete
  4. good blog.........

    ReplyDelete
  5. but my program is overriding the static method. what wrong with it :-p
    static class SuperClass {

    public static void Static(){
    System.out.println("parent");
    }
    }

    static class SubClass extends SuperClass{

    public static void Static(){
    System.out.println("child");
    }
    }

    ReplyDelete
    Replies
    1. This program will not compile bcoz You can use static keyword only with ineer classes.

      Delete
    2. Absolutely right! Outer class can never be static in Java.

      @ AnonymousDecember 15, 2013 at 8:24 AM :

      Try the example that Javin explained but this time make the static methods as non static in both Screen and ColourScreen classes i.e. public void show(){.....}

      Rest of the things keep as they are and then see the output. It will give you:
      Overridden static method in Child Class in Java

      If you compare this o/p with the program o/p that Javin explained then you will see that overriding is not done in first case but in the second... :)
      Hope this helps,
      RaHuL

      Delete

Java67 Headline Animator