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 syntax to call hidden static method in a Java program is as follows:

Syntax:

superClassName.staticMethodName

Rules of Method Hiding in Java


All the rules of method hiding are exactly the same as overriding, except one rule.

1. Both parent and child class methods must be static.

Difference between method overriding and method hiding concepts in Java

Features of Method Hiding in Java


1. Method hiding is also known as compile-time polymorphism because the compiler is responsible for resolving 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 Java compiler based on the 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


Let’s take some example programs to understand Java method hiding concept better.

Example 1:

package methodHidingPractice; 
public class ParentClass 
{ 
// Declare a static method. 
   public static void classMethod()
   { 
      System.out.println("classMethod in Parent class"); 
   } 
// Declare an instance method.
   public void instanceMethod()
   { 
      System.out.println("instanceMethod in Parent class"); 
   } 
} 
public class ChildClass extends ParentClass 
{ 
// Redefining the static method of superclass in the subclass.
   public static void classMethod()
   { 
      System.out.println("classMethod in Child class"); 
   }
// Overriding the method of superclass. 
   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

In this example, we have defined a class named ParentClass that contains one static method and one instance method. Both methods will print a message on the console. Then, we have defined another class named ChildClass that extends the ParentClass.

Inside the subclass ChildClass, we have redefined the static method that will hide the static method of the superclass. However, we are overriding the instance method in the subclass.

The statement p.classMethod(); will call the method of ParentClass because in the method hiding, the method call is resolved by Java compiler based on the type of reference.

The statement p.instanceMethod(); will call the method of ChildClass because in method overriding, the method call is resolved by JVM at runtime object. Since the reference variable p is pointing to the object of subclass, therefore, it will call the method of subclass.

Similarly, the statement c.classMethod(); will call the method of ChildClass because the reference variable c is a type of ChildClass. The statement c.instanceMethod(); will invoke the method of ChildClass because the reference variable c is pointing to the object of ChildClass.


Key points:

1. In the method overriding, the method call is resolved by JVM based on the runtime object (new Object).

2. In the method hiding, the method call is resolved by the Java compiler based on reference type (Object obj).


Example 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

Explanation:

1. x.m1(10); will call m1() method of class X because x is a reference type of class X.

2. y.m1(20); will give a compile-time error because we cannot reduce the visibility of the inherited method from class X.

Difference between Method Hiding and Method Overriding


There are the following differences between method hiding and method overriding in Java. They are:

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 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 take a simple example program where we will hide a variable named x in subclass while it is already defined by its superclass.

Example 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 a subclass with the same name. 
   String x = "Hi"; 

   void display()
   { 
      System.out.println(x); 
   // If you still want to access variable x from the superclass, 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, the instance variable is chosen from the reference type.

Let’s understand this concept with the help of an example program. Look at the program code.

Example 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 we 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.

Let’s take a simple example program where we will hide an instance variable by local variable.

Example 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

Key Points to Remember

1. When superclass and subclass contain static methods with the same signature, then subclass static method hides superclass static method. This is called method hiding in Java.

2. In method hiding concept, the method call depends on the reference type.

3. It is also known as compile-time polymorphism.