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.

Variable Hiding in Java


When a variable defined in the parent class is redefined with the same name in a child class, the child class's variable hides the variable defined in the parent class. This mechanism is called variable hiding in Java. It can be achieved by declaring a variable in the child class that must be the same name and type of an inherited variable from its parent class.

Variable hiding is useful when you want to reuse the same variable name in the subclass. Let's see an example program where we are hiding the variable named x in subclass while it is already defined by its superclass.
Program source code 3:
    package variableHiding; public class AA { String x="Hello"; } public class BB extends AA { // Hiding super class's variable `x` by defining a variable in subclass with the same name. String x="Hi"; void display(){ System.out.println(x); // If you still want to access variable x from the super class, you can access by using `super.x` System.out.print(super.x); } public static void main(String[] args) { BB a=new BB(); a.display(); } }
    Output: Hi Hello
When an instance variable in a child class has the same name as an instance variable in a parent class, instance variable is chosen from the reference type. Let's understand this concept by an example program.
Program source code 4:
    package variableHiding; public class A { int x=10; } public class B extends A{ int x=20; public static void main(String[] args) throws Exception { A a = new A(); System.out.println(a.x); // Output - 10, "Parent's Instance Variable" B b = new B(); System.out.println(b.x); // Output - 20, "Child's Instance Variable" A a1=new B(); System.out.println(a1.x); // Output- 10, "Child's Instance Variable". } }
    Output: 10 20 10

Instance Variable Hiding in Java


In Java programming, when you declare a local variable in a method with the same name as an instance variable, then the local variable hides instance variable. This concept is useful if you want to reflect the change made over to the instance variable.
Program source code 5:
    package variableHiding; public class Test { // Declaration of instance variable. private int x = 10; void show() { // This local variable hides instance variable int x = 40; System.out.println("Value of Instance variable: " + this.x); System.out.println("Value of Local variable: " +x); } } public class MyTest { public static void main(String[] args) { Test t=new Test(); t.show(); } }
    Output: Value of Instance variable: 10 Value of Local variable: 40
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!
Next ➤ Dynamic Method Dispatch in Java