Method Hiding in Java


A static method (class method) cannot be overridden in Java but if a static method defined in the parent class is redefined in a child class, the child class's method hides the method defined in the parent class. This mechanism is called method hiding in Java or function hiding. The hidden static method can be called using the syntax superClassName.staticMethodName.  

Rules of Method Hiding in Java


All rules of method hiding are exactly the same as overriding except one rule.
1. Both parent and child class methods must be static.
Method Hiding in Java

Features of Method Hiding in Java


1. Method hiding is also known as compile-time polymorphism because the compiler is responsible to resolve method resolution based on the reference type.
2. It is also known as static polymorphism or early binding.
3. In method hiding, method call is always resolved by compiler based on reference type. There is no role of runtime polymorphism in method hiding in java.
4. The use of static in method declaration must be the same between superclass and subclass.

Method Hiding and Method Overriding Example Program


Let's see practical example programs to understand the method hiding concept.
Program source code 1:
    package methodHidingPractice; public class ParentClass { public static void classMethod(){ System.out.println("classMethod in Parent class"); } public void instanceMethod(){ System.out.println("instanceMethod in Parent class"); } } public class ChildClass extends ParentClass { public static void classMethod(){ System.out.println("classMethod in Child class"); } public void instanceMethod(){ System.out.println("instanceMethod in Child class"); } } public class MyClass { public static void main(String[] args) { ParentClass p=new ChildClass(); p.classMethod(); // Calling with reference. (Method hiding) p.instanceMethod(); // Calling with object. (Method overriding) ChildClass c=new ChildClass(); c.classMethod(); // Calling with reference. c.instanceMethod(); // Calling with object. ParentClass p1=new ParentClass(); p1.classMethod(); // Calling with reference. p1.instanceMethod(); // Calling with object. } }
    Output: classMethod in Parent class instanceMethod in Child class classMethod in Child class instanceMethod in Child class classMethod in Parent class instanceMethod in Parent class
Key points: 
1. In method overriding, method call is resolved by JVM based on runtime object (new Object).
2. In method hiding, method call is resolved by the compiler based on reference type (Object obj).
Program source code 2:
    package methodHidingPractice; public class X { protected static void m1(int a){ System.out.println("m1-X"); } } public class Y extends X{ static void m1(int y){ System.out.println("m1-Y"); } } public class MyTest { public static void main(String[] args) { X x=new Y(); x.m1(10); Y y=new Y(); y.m1(20); } }
    Output: m1-X Compile-time error
Output Explanation: 
1. x.m1(10); will call m1() method of class X because x is a reference type of X.
2. y.m1(20); will give a compile-time error because we cannot reduce the visibility of the inherited method from X.

Difference between Method Hiding & Method Overriding in Java


There are the following differences between method hiding and method overriding in Java.
1. In method hiding, both parent and child class methods should be static whereas, in overriding, both parent and child class methods should be non-static.

2. Compiler is responsible for method resolution based on reference type whereas, in method overriding, JVM is always responsible for method resolution based on runtime object.

3. Method hiding is also known as compile-time polymorphism, static polymorphism, or early binding whereas, method overriding is also known as runtime polymorphism, dynamic polymorphism, or late binding.

Final words 
Hope that this tutorial has covered all the important section in method hiding in java with example programs. I hope that you will have successfully performed practical example programs and enjoyed them. 
Thanks for reading!