BCA / B.Tech 9 min read

Method Hiding in Java

What is Method Hiding in Java?

Method hiding is a concept that applies specifically to static methods in an inheritance hierarchy. If a subclass defines a static method that has the same signature (name and parameter list) as a static method in its superclass, the subclass's method hides the superclass's method instead of overriding it.

The key difference is how the method call is resolved. Method hiding uses compile-time binding (static binding), meaning the method that gets called is determined by the reference type, not the object type at runtime.


Method Hiding vs. Method Overriding:
Feature Method Hiding Method Overriding
Applies to static methods Instance (non-static) methods
Binding Compile-time (Static) Run-time (Dynamic)
Resolution Based on the reference type Based on the object type

Example:

class Parent {
    static void display() {
        System.out.println("Static method from Parent");
    }
}

class Child extends Parent {
    // This static method HIDES the one in the Parent class
    static void display() {
        System.out.println("Static method from Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        
        // The reference type is Parent, so the Parent's static method is called.
        // The fact that `p` points to a Child object is irrelevant for static methods.
        p.display(); 
    }
}
        
Output:
Static method from Parent