Lesson Completion
Back to course

Method Hiding: Static Methods in Inheritance

Intermediate
18 minutes4.5Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: Method hiding occurs when a subclass defines a static method with the same signature as a static method in the superclass. Unlike overriding (which applies to instance methods), static methods are resolved at compile-time based on the reference type, not the object type. The child method "hides" the parent method.

Think of company policies. A parent company has a static policy manual. When a subsidiary creates its own manual with the same name, it doesn't override the parent's—it "hides" it. Which one you see depends on which office you're in (reference type)!


2. Conceptual Clarity (The "Simple" Tier)

💡 The Analogy: The Menu Boards

  • Corporate HQ Menu (Parent static method): Fixed menu board
  • Franchise Location Menu (Child static method): Their own fixed menu board
  • Which menu you see depends on which location you're standing in (reference type), not which company owns it (object type)

Hand-Drawn Logic Map

graph LR A[Parent.staticMethod] -.hides.-> B[Child.staticMethod] C[Parent ref → Child obj] --> D{Compile-time check} D --> E[Calls Parent.staticMethod] style E fill:#BF360C

3. Technical Mastery (The "Deep Dive")

Formal Definition

Method Hiding:

  • Occurs when a subclass defines a static method with the same signature as a static method in the superclass
  • Resolution is compile-time (early binding)
  • The method called depends on the reference type, not the object type
  • This is NOT overriding (which is runtime polymorphism)

The "Why" Paragraph

Why can't static methods be overridden? Static methods belong to the class, not instances. Polymorphism requires runtime determination based on the actual object—but static methods don't have objects! They're called on the class itself. Hence, Java uses compile-time binding for static methods, which is why we call it "hiding" instead of "overriding."

Visual Architecture

sequenceDiagram participant Code participant Compiler Code->>Compiler: Parent ref = new Child() Code->>Compiler: ref.staticMethod() Note over Compiler: Checks REFERENCE type (Parent) Compiler->>Code: Calls Parent.staticMethod()

4. Interactive & Applied Code

The "Perfect" Code Block

java
class Parent { static void display() { System.out.println("Static method in Parent"); } void show() { // Instance method System.out.println("Instance method in Parent"); } } class Child extends Parent { // This HIDES Parent.display(), doesn't override it static void display() { System.out.println("Static method in Child"); } // This OVERRIDES Parent.show() @Override void show() { System.out.println("Instance method in Child"); } } public class Main { public static void main(String[] args) { Parent parent = new Parent(); Parent childAsParent = new Child(); // Upcasting Child child = new Child(); // STATIC METHOD (Method Hiding) parent.display(); // Parent's static childAsParent.display(); // Parent's static (reference type!) child.display(); // Child's static // Better: call via class name Parent.display(); // Parent's static Child.display(); // Child's static // INSTANCE METHOD (Overriding - Polymorphism) parent.show(); // Parent's instance childAsParent.show(); // Child's instance (object type!) child.show(); // Child's instance } } /* Output: Static method in Parent Static method in Parent <-- Note: Parent's version! Static method in Child Static method in Parent Static method in Child Instance method in Parent Instance method in Child <-- Note: Child's version (polymorphism)! Instance method in Child */

The "Anti-Pattern" Example

❌ Calling Static Methods on Instances

java
Parent ref = new Child(); ref.display(); // ❌ Confusing! Looks like polymorphism but isn't

✅ Always Call Static Methods on Classes:

java
Parent.display(); // Clear! Child.display(); // Clear!

5. The Comparison & Decision Layer

Versus Table: Hiding vs. Overriding

FeatureMethod Hiding (Static)Overriding (Instance)
Method TypeStaticInstance (non-static)
BindingCompile-timeRuntime
Determined ByReference typeObject type
PolymorphismNOYES

6. The "Interview Corner" (The Edge)

The "Killer" Interview Question: "What's the difference between method hiding and method overriding?" Answer:

  • Overriding: Instance methods, runtime polymorphism, determined by object type
  • Hiding: Static methods, compile-time binding, determined by reference type

You can't override static methods because they belong to the class, not instances!

JVM Note

No vtable for Static Methods: The JVM doesn't use the virtual method table for static methods. They're resolved directly at compile time using the reference type, making them slightly faster but non-polymorphic.

Pro-Tip: Avoid "hiding" static methods intentionally. It's confusing and error-prone. If a child class needs different static behavior, use a different method name to make intent clear!

Topics Covered

Object-Oriented ProgrammingInheritance

Tags

#java#inheritance#polymorphism#super-class#sub-class#interview-prep

Last Updated

2025-02-01