The principle of runtime polymorphism doesn’t apply to static member methods. For non-static methods, if the sub class also defines a method with same signature as the super class method, then it is considered as overridden. A super class reference referring to subclass instance will result in invocation of sub class method version.

However when a static member method is defined in the sub class with same signature as the super class static method then results in method hiding. Now if we have super class reference variable referring to sub class object, the invocation of static method will result in calling the super class version. Thus the basic principle to keep in mind is that run time polymorphism is applicable for non-static member methods only and not for static methods. For static methods, the redeclaration of method in sub class results in method hiding.

Sample code

showing the concept of static method hiding follows:

MyTest.java

package com.example;

public class MyTest {
	
	static void test() {
		System.out.println("mytest");
	}
}

Test.java

package com.example;

public class Test extends MyTest{
 
	static void test() {
		System.out.println("test");
	}
	
	public static void main(String[] args) {
		MyTest t = new Test();
		t.test();
		
	}
}

Output:

mytest

The reason for above output is that the static members are associated with class as a whole and not instances. Hence the binding of the static method is done at compile time only. To confirm the above sentence, let us see what the compiler has done to our code in the main method of class Test. Here is decompiled code of class Test:

De-compiled code

package com.example;

import java.io.PrintStream;

// Referenced classes of package com.example:
//            MyTest

public class Test extends MyTest
{

    public Test()
    {
    }

    static void test()
    {
        System.out.println("test");
    }

    public static void main(String args[])
    {
        MyTest t = new Test();
        MyTest.test();
    }
}

Here we can notice that the call t.test() has been replaced with MyTest.test(). Due to the above conversion by compiler, there is no question about run time polymorphism coming into picture with static methods and hence we see method hiding for static methods.

Do we have runtime polymorphism with static methods? admin Core Java
The principle of runtime polymorphism doesn't apply to static member methods. For non-static methods, if the sub class also defines a method with same signature as the super class method, then it is considered as overridden. A super class reference referring to subclass instance will result in invocation of...
The principle of runtime polymorphism doesn't apply to <a href="http://www.javaexperience.com/static-analysis-in-eclipse-using-java/" title="Static Analysis in Eclipse using Java">static</a> member methods. For non-static methods, if the sub class also defines a method with same signature as the super class method, then it is considered as overridden. A super class reference referring to subclass instance will result in invocation of sub class method version. However when a static member method is defined in the sub class with same signature as the <a href="http://www.javaexperience.com/inheriting-collections-to-create-custom-collections/" title="Inheriting Collections classes to make custom classes">super class</a> static method then results in method hiding. Now if we have super class reference variable referring to sub class object, the invocation of static method will result in calling the super class version. Thus the basic principle to keep in mind is that run time polymorphism is applicable for non-static member methods only and not for static methods. For static methods, the redeclaration of method in sub class results in method hiding. <h2>Sample code</h2> showing the concept of static method hiding follows: <strong>MyTest.java</strong> 1 <strong>Test.java</strong> 1 Output: <code>mytest</code> The reason for above output is that the static members are associated with class as a whole and not instances. Hence the binding of the static method is done at compile time only. To confirm the above sentence, let us see what the compiler has done to our code in the main method of class Test. Here is decompiled code of class Test: <h2>De-compiled code</h2> 1 Here we can notice that the call t.test() has been replaced with MyTest.test(). Due to the above conversion by compiler, there is no question about run time polymorphism coming into picture with static methods and hence we see method hiding for static methods.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.