The Wayback Machine - https://web.archive.org/web/20230806054637/https://www.geeksforgeeks.org/method-hiding-in-java/
Skip to content
Related Articles
Open In App

Related Articles

Method Hiding in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Prerequisite: Overriding in Java

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass. This mechanism happens because the static method is resolved at the compile time. Static method bind during the compile time using the type of reference not a type of object.

Examples

Java




// Java program to demonstrate
// method Hiding in java
 
// Base Class
class Complex {
    public static void f1()
    {
        System.out.println(
            "f1 method of the Complex class is executed.");
    }
}
 
// class child extend Demo class
class Sample extends Complex {
    public static void f1()
    {
        System.out.println(
            "f1 of the Sample class is executed.");
    }
}
public class Main {
 
    public static void main(String args[])
    {
        Complex d1 = new Complex();
 
        // d2 is reference variable of class Demo that
        // points to object of class Sample
        Complex d2 = new Sample();
 
        // But here method will be call using type of
        // reference
        d1.f1();
        d2.f1();
    }
}

Output

f1 method of the Complex class is executed.
f1 method of the Complex class is executed.

Program Demonstrate the Difference Between Method Overriding and Method Hiding in Java:

Java




// Java program to demonstrate
// Difference between method overriding and Method hiding
 
// Base Class
class Complex {
    public static void f1()
    {
        System.out.println(
            "f1 method of the Complex class is executed.");
    }
 
    public void f2()
    {
        System.out.println(
            "f2 method of the Complex class is executed.");
    }
}
 
// class child extend Demo class
class Sample extends Complex {
    public static void f1()
    {
        System.out.println(
            "f1 of the Sample class is executed.");
    }
    public void f2()
    {
        System.out.println(
            "f2 method of the Sample class is executed.");
    }
}
public class Main {
 
    public static void main(String args[])
    {
        Complex d1 = new Complex();
         
         // d2 is the reference variable of class Demo that
        // points to object of class Sample
        Complex d2 = new Sample();
         
        // But here method will be called using type of
        // reference
        d1.f1();
        d2.f1();
 
        // ***** Function overriding in java *****//
        // method refer to the  object  in the method
        // overriding.
        d1.f2();
        d2.f2();
    }
}

Output

f1 method of the Complex class is executed.
f1 method of the Complex class is executed.
f2 method of the Complex class is executed.
f2 method of the Sample class is executed.

Difference Between Method Overriding and Method Hiding in Java

  • In method overriding both the method parent class and child class are non-static.
  • In method Hiding both the method parent class and child class are static.
  • In method Overriding method resolution is done on the basis of the Object type.
  • In method Hiding method resolution is done on the basis of reference type.
  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass. 

Last Updated : 02 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials